Snippets

Gravitywell 𐅉.js

Created by Lucian Buzzo last modified
;(function() {
  var root = this;

  function toArray(val) {
    return Array.prototype.slice.call(val);
  }

  function forEach(collection, fn) {
    var i = 0;
    for(i; i < collection.length; i++) {
      fn(collection[i]);
    }
  }

  function map(collection, fn) {
    var i = 0;
    var newCollection = [];
    for(i; i < collection.length; i++) {
      newCollection.push(fn(collection[i]));
    }
    return newCollection;
  }

  function filter(collection, fn) {
    var i = 0;
    var newCollection = [];
    for(i; i < collection.length; i++) {
      if (fn(collection[i])) {
        newCollection.push(collection[i]);
      }
    }
    return newCollection;
  }

  function isUndefined(value) {
    return typeof value == 'undefined';
  }

  function has(object, key) {
    return object ? hasOwnProperty.call(object, key) : false;
  }

  function keys(object) {
    var array = [];
    for (var key in object) {
      if (object.hasOwnProperty(key)) {
        array.push(key);
      }
    }
    return array;
  }

  function isString(value) {
    return typeof value == 'string';
  }

  function isArray(value) {
    return Object.prototype.toString.call(value) === '[object Array]';
  }

  function pick(object, keys) {
    var newObject = {};
    keys = (isString(keys) ? [keys] : keys);
    forEach(keys, function(v) {
      if (object.hasOwnProperty(v)) {
        newObject[v] = clone(object[v]);
      }
    });
    return newObject;
  }

  function isObject(value) {
    // Avoid a V8 JIT bug in Chrome 19-20.
    // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
    var type = typeof value;
    return type == 'function' || (value && type == 'object') || false;
  }

  function clone(value) {
    return JSON.parse(JSON.stringify(value));
  }

  var chainFunctions = [map, forEach, isUndefined, has, keys, clone, filter, pick, isString, isArray];

  var BalsaConstructor = function() {
    function balsa(val) {
      var methods = {};
      if(val !== undefined) {
        forEach(chainFunctions, function(chainable) {
          methods[chainable.name] = function() {
            var argsArray = toArray(arguments);
            argsArray.unshift(val);
            return chainable.apply(null, argsArray);
          };
        });

        return methods;
      }
    }

    balsa.map = map;
    balsa.forEach = forEach;
    balsa.filter = filter;

    balsa.isUndefined = isUndefined;
    balsa.isString = isString;
    balsa.isArray = isArray;

    balsa.has = has;
    balsa.keys = keys;
    balsa.clone = clone;
    balsa.pick = pick;

    return balsa;
  };

  root['𐅉'] = root.b = new BalsaConstructor();

}.call(this));
(function(){function n(n){return Array.prototype.slice.call(n)}function r(n,r){var t=0;for(t;t<n.length;t++)r(n[t])}function t(n,r){var t=0,e=[];for(t;t<n.length;t++)e.push(r(n[t]));return e}function e(n,r){var t=0,e=[];for(t;t<n.length;t++)r(n[t])&&e.push(n[t]);return e}function u(n){return"undefined"==typeof n}function i(n,r){return n?hasOwnProperty.call(n,r):!1}function o(n){var r=[];for(var t in n)n.hasOwnProperty(t)&&r.push(t);return r}function f(n){return"string"==typeof n}function c(n){return"[object Array]"===Object.prototype.toString.call(n)}function a(n,t){var e={};return t=f(t)?[t]:t,r(t,function(r){n.hasOwnProperty(r)&&(e[r]=s(n[r]))}),e}function s(n){return JSON.parse(JSON.stringify(n))}var l=this,p=[t,r,u,i,o,s,e,a,f,c],h=function(){function l(t){var e={};return void 0!==t?(r(p,function(r){e[r.name]=function(){var e=n(arguments);return e.unshift(t),r.apply(null,e)}}),e):void 0}return l.map=t,l.forEach=r,l.filter=e,l.isUndefined=u,l.isString=f,l.isArray=c,l.has=i,l.keys=o,l.clone=s,l.pick=a,l};l["𐅉"]=l.b=new h}).call(this);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.