Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

[Nexus] Code Snippets

TysandrTysandr Posts: 441Member ✭✭✭✭ - Eminent
Making one for Nexus:

Purpose:
Grabbing css values of widths/heights, etc and turning them into numbers

Requirements:
none

Code:
_clean = function(n) {
  if (typeof n !== 'string') { return n }
  var x = Number( n.replace(/[^-\d\.]/g,''))
  return x }

Output:
Turns '394px' into 393 (integer)
Nexus: Accents, Autotriggers, Dragon Talismans
"As the child did... without the rope. Then fear will find you again."

Comments

  • TysandrTysandr Posts: 441Member ✭✭✭✭ - Eminent
    edited March 12
    Purpose:
    Pads strings with extra characters

    Requirements:
    none

    Code:
    _lpad = function(str, len, ch) {
       if (typeof str == 'number') { str = str.toString() }
       if (ch == null) { ch = ' ' }
       var r = len - str.length
       if (r < 0) { r = 0 }
       return ch.repeat(r) + str }
    _rpad = function(str, len, ch) {
       if (typeof str == 'number') { str = str.toString() }
       if (ch == null) { ch = ' ' }
       var r = len - str.length
       if (r < 0) { r = 0 }
       return str + ch.repeat(r)  }

    Output:
    _lpad('tornado', 12, '.') == '.....tornado'
    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

  • TysandrTysandr Posts: 441Member ✭✭✭✭ - Eminent
    Purpose:
    Prints out Javascript onto the main window

    Requirements:
    none

    Code:
    _display = function(a) {
      var r = function(k,v) {
       if (typeof v === 'function') {
         var q = '' + v
             q = q.substring(0, 79)
         return 'FUNCTION >>   ' + q }
       return v }
      var x = JSON.stringify(a, r, 3)
      ow_Write('<span class="normal">' + x + '</span><br>') }

    Output:
    As described
    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

  • TysandrTysandr Posts: 441Member ✭✭✭✭ - Eminent
    Purpose:
    Finding the interval between two Date objects

    Requirements:
    none

    Code:
    _interval = function(a,b) {
      if (!a) { return 0 }
      var a = a
      var b = b || new Date()
      if (b > a) { b = [a, a = b][0] } // swap variable contents
      var diff  = a.getTime() - b.getTime()
      var msecs = diff % 1000
      var secs  = Math.floor(diff / 1000)
      var mins  = Math.floor(secs / 60)
          secs  = secs % 60
      var hrs   = Math.floor(mins / 60)
          mins  = mins % 60
      var days  = Math.floor(hrs  / 24)
           hrs  =  hrs % 24
      var s = {}
          s.msecs = msecs
          s.secs  = secs
          s.mins  = mins
          s.hrs   = hrs
          s.days  = days
      return s }

    Output:
    Turns DateA - DateB into {days:0, hrs:4, mins: 35, secs:12, msecs: 140} for instance.
    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

  • TysandrTysandr Posts: 441Member ✭✭✭✭ - Eminent
    Purpose:
    Download data onto computer.

    Requirements:
    none

    Code:
    _save = function(datum, filename) {
      var f = function() {
       var data = ''
       for (var i=0; i<datum.length; i++) {
         data += datum[i]   
       }
       var x = new Blob([data], { type: 'text/plain' })
       var t = window.URL.createObjectURL(x)
       return t }
      
      var a = document.createElement('a')
          a.setAttribute('download', filename)
    a.href = f() document.body.appendChild(a) window.requestAnimationFrame( function() { var e = new MouseEvent('click') a.dispatchEvent(e) document.body.removeChild(a) }) }

    Output:
    Turns data into strings and saves it into a file as filename without need for mouse navigation.
    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

Sign In to Comment.