underscore-awesomer
Version:
Awesome Extensions to Underscore.js (or Lo-Dash).
5 lines • 6.82 kB
JavaScript
/*
Underscore-Awesomer.js 1.2.4
(c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/underscore-awesomer/
License: MIT (http://www.opensource.org/licenses/mit-license.php)
*/(function(){return function(e){return typeof define=="function"&&define.amd?define("underscore-awesomer",["underscore"],e):e.call(this)}(function(){var e,t;e=this;if(!this._&&typeof require!="undefined")try{t=require("lodash")}catch(n){t=require("underscore")}else t=this._;return t&&t.hasOwnProperty("_")&&(t=t._),typeof e.exports!="undefined"&&(e.exports=t),t.AWESOMENESS="1.2.4",t.pluck=function(e,n,r){return r?t.map(e,function(e){var t;return t=e[n],delete e[n],t}):t.map(e,function(e){return e[n]})},t.remove=function(e,n,r){var i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M,D,P;r==null&&(r={});if(t.isEmpty(e))return!n||t.isFunction(n)?[]:void 0;t.isFunction(r)&&(r={callback:r});if(r.preclear){f=e,e=t.clone(e);if(t.isArray(f))f.length=0;else for(s in f)delete f[s]}l=[],c=!1;if(t.isArray(e)){if(t.isUndefined(n))l=t.keys(e);else if(t.isFunction(n))r.first_only?(c=!0,t.find(e,function(e,t){return n(e)?(l.push(t),!0):!1})):t.each(e,function(e,t){if(n(e))return l.push(t)});else if(t.isArray(n))if(r.first_only){c=!0;for(d=0,y=n.length;d<y;d++){u=n[d];for(i in e){h=e[i];if(u===h){l.push(i);break}}}}else for(v=0,b=n.length;v<b;v++){u=n[v];for(i in e)h=e[i],u===h&&l.push(i)}else if(r.first_only)c=!0,i=t.indexOf(e,n),i>=0&&l.push(i);else{c=!0;for(i in e)h=e[i],n===h&&l.push(i)}if(c){if(!l.length)return void 0;h=e[l[0]],l=l.sort(function(e,n){return t.compare(n,e)});for(m=0,w=l.length;m<w;m++)i=l[m],e.splice(i,1);if(r.callback)for(g=0,E=l.length;g<E;g++)i=l[g],r.callback(h);return h}if(!l.length)return[];p=[],l=l.sort(function(e,n){return t.compare(n,e)});for(L=0,S=l.length;L<S;L++)i=l[L],p.unshift(e[i]),e.splice(i,1);if(r.callback)for(A=0,x=p.length;A<x;A++)h=p[A],r.callback(h);return t.uniq(p)}if(t.isUndefined(n))l=t.keys(e);else if(t.isFunction(n))for(s in e)h=e[s],n(h,s)&&l.push(s);else if(t.isArray(n))if(r.values)for(O=0,T=n.length;O<T;O++){u=n[O];if(r.first_only)for(s in e){h=e[s];if(u===h){l.push(s);break}}else for(s in e)h=e[s],u===h&&l.push(s)}else{a=n;for(M=0,N=n.length;M<N;M++)o=n[M],e.hasOwnProperty(o)&&l.push(o)}else if(t.isString(n)&&!r.values)c=!0,a=[],e.hasOwnProperty(n)&&(a.push(n),l.push(n));else for(s in e)h=e[s],n===h&&l.push(s);if(a){if(!a.length)return c?void 0:[];p=[];for(D=0,C=l.length;D<C;D++)s=l[D],p.push(e[s]),delete e[s];if(r.callback)for(i in p)h=p[i],r.callback(h,a[i]);return c?p[0]:p}if(!l.length)return{};p={};for(P=0,k=l.length;P<k;P++)s=l[P],p[s]=e[s],delete e[s];if(r.callback)for(s in p)h=p[s],r.callback(h,s);return p},t.findIndex=function(e,t){var n,r;for(n in e){r=e[n];if(t(e[n]))return n}return-1},t.hasKeypath=t.keypathExists=function(e,n){return!!t.keypathValueOwner(e,n)},t.keypathValueOwner=function(e,n){var r,i,s,o,u;r=t.isString(n)?n.split("."):n,i=e,u=r.length;for(s in r){o=r[s];if(!(o in i))break;if(++s===u)return i;i=i[o];if(!i||!(i instanceof Object))break}return void 0},t.keypath=function(e,n,r){var i,s;i=t.isString(n)?n.split("."):n,s=t.keypathValueOwner(e,i);if(arguments.length===2)return s?s[i[i.length-1]]:void 0;if(!s)return;return s[i[i.length-1]]=r,r},t.cloneToDepth=t.containerClone=t.clone=function(e,n){var r,i;if(!e||typeof e!="object")return e;if(t.isArray(e))r=Array.prototype.slice.call(e);else{if(e.constructor!=={}.constructor)return e;r=t.extend({},e)}if(!t.isUndefined(n)&&n>0)for(i in r)r[i]=t.clone(r[i],n-1);return r},t.deepClone=function(e,n){var r,i;if(!e||typeof e!="object")return e;if(t.isString(e))return String.prototype.slice.call(e);if(t.isDate(e))return new Date(e.valueOf());if(t.isFunction(e.clone))return e.clone();if(t.isArray(e))r=Array.prototype.slice.call(e);else{if(e.constructor!=={}.constructor)return e;r=t.extend({},e)}if(!t.isUndefined(n)&&n>0)for(i in r)r[i]=t.deepClone(r[i],n-1);return r},t.isConstructor=function(e){return t.isFunction(e)&&e.name},t.resolveConstructor=function(n){var r,i;return r=t.isArray(n)?n:t.isString(n)?n.split("."):void 0,r?(i=r.length===1?e[r[0]]:t.keypath(e,r),i&&t.isConstructor(i)?i:void 0):t.isFunction(n)&&t.isConstructor(n)?n:void 0},t.CONVERT_NONE=0,t.CONVERT_IS_TYPE=1,t.CONVERT_TO_METHOD=2,t.conversionPath=function(e,n){var r,i,s,o;i=t.isArray(n)?n:t.isString(n)?n.split("."):void 0,o=typeof e,r=i?i[i.length-1]:void 0,o=typeof e,r=i?i[i.length-1]:void 0;if(i&&o===r)return t.CONVERT_IS_TYPE;s=t.resolveConstructor(i?i:n);if(s&&o==="object")try{if(e instanceof s)return t.CONVERT_IS_TYPE}catch(u){({})}return r=s&&s.name?s.name:r,r?t["is"+r]&&t["is"+r](e)?t.CONVERT_IS_TYPE:o==="object"&&e["to"+r]?t.CONVERT_TO_METHOD:t.CONVERT_NONE:t.CONVERT_NONE},t.isConvertible=function(e,n){return t.conversionPath(e,n)>0},t.toType=function(e,n){var r,i;r=t.isArray(n)?n:t.isString(n)?n.split("."):void 0;switch(t.conversionPath(e,r?r:n)){case 1:return e;case 2:return r?e["to"+r[r.length-1]]():(i=t.resolveConstructor(n),i&&i.name?e["to"+i.name]():void 0)}return void 0},t.functionExists=function(e,n){return e instanceof Object&&e[n]&&t.isFunction(e[n])},t.callIfExists=function(e,n){return t.functionExists(e,n)?e[n].apply(e,Array.prototype.slice.call(arguments,2)):void 0},t.getSuperFunction=function(e,n){var r;return r=t.keypathValueOwner(e,["constructor","__super__",n]),r&&t.isFunction(r[n])?r[n]:void 0},t.superCall=function(e,n){var r;return r=t.getSuperFunction(e,n),r?r.apply(e,Array.prototype.slice.call(arguments,2)):void 0},t.superApply=function(e,n,r){var i;return i=t.getSuperFunction(e,n),i?i.apply(e,r):void 0},t.classOf=function(e){return e!=null&&Object.getPrototypeOf(Object(e)).constructor.name||void 0},t.copyProperties=function(e,n,r,i){var s,o,u,a,f;u=r||t.keys(n),s=!1;for(a=0,f=u.length;a<f;a++){o=u[a];if(!hasOwnProperty.call(n,o))continue;e[o]=n[o],s=!0,i&&delete n[o]}return s},t.getValue=function(e,t,n,r){var i;return hasOwnProperty.call(e,t)?r?(i=e[t],delete e[t],i):e[t]:n},t.sortBy=function(e,n,r){return t.pluck(t.map(e,function(e,t,i){return{value:e,criteria:n.call(r,e,t,i)}}).sort(function(e,n){var r,i;return r=e.criteria,i=n.criteria,t.compare(r,i)}),"value")},t.sortedIndex=function(e,n,r){var i,s,o;r||(r=t.identity),s=0,i=e.length;while(s<i)o=s+i>>1,t.compare(r(e[o]),r(n))===t.COMPARE_ASCENDING?s=o+1:i=o;return s},t.COMPARE_EQUAL=0,t.COMPARE_ASCENDING=-1,t.COMPARE_DESCENDING=1,t.compare=function(e,n,r){var i;return typeof e!="object"?e===n?t.COMPARE_EQUAL:e<n?t.COMPARE_ASCENDING:t.COMPARE_DESCENDING:(r||(r="compare"),e[r]&&t.isFunction(e[r])?(i=e[r](n),i===0?t.COMPARE_EQUAL:i<0?t.COMPARE_ASCENDING:t.COMPARE_DESCENDING):n[r]&&t.isFunction(n[r])?(i=n[r](e),i===0?t.COMPARE_EQUAL:i<0?t.COMPARE_DESCENDING:t.COMPARE_ASCENDING):e===n?t.COMPARE_EQUAL:e<n?t.COMPARE_ASCENDING:t.COMPARE_DESCENDING)},t})}).call(this);