[Nexus] Code Snippets

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 }<br>

Output:
Turns '394px' into 393 (integer)
"All we have to decide is what to do with the time that is given to us."

Comments

  • edited March 2018
    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'
    "All we have to decide is what to do with the time that is given to us."

  • 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>') }<br>

    Output:
    As described
    "All we have to decide is what to do with the time that is given to us."

  • 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 }<br>

    Output:
    Turns DateA - DateB into {days:0, hrs:4, mins: 35, secs:12, msecs: 140} for instance.
    "All we have to decide is what to do with the time that is given to us."

  • 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)<br>      a.href = f()
      document.body.appendChild(a)
      window.requestAnimationFrame( function() {
       var e = new MouseEvent('click')
       a.dispatchEvent(e)
       document.body.removeChild(a)
      })  }<br>

    Output:
    Turns data into strings and saves it into a file as filename without need for mouse navigation.
    "All we have to decide is what to do with the time that is given to us."

Sign In or Register to comment.