Created
April 28, 2016 14:54
-
-
Save jimmed/9d7228faaf9aa2e36a3ad148fa6b52e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var toString = Function.prototype.toString | |
var hasOwnProperty = Object.prototype.hasOwnProperty | |
var regexpCharacters = /[\\^$.*+?()[\]{}|]/g | |
var regexpIsNativeFn = toString.call(hasOwnProperty) | |
.replace(regexpCharacters, '\\$&') | |
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') | |
var regexpIsNative = RegExp('^' + regexpIsNativeFn + '$') | |
function toSource (func) { | |
if (!func) return '' | |
try { | |
return toString.call(func) | |
} catch (e) {} | |
try { | |
return (func + '') | |
} catch (e) {} | |
} | |
var forEach = Array.prototype.forEach | |
var assign = Object.assign | |
var filter = Array.prototype.filter | |
var find = Array.prototype.find | |
var indexOf = Array.prototype.indexOf | |
var keys = Object.keys | |
var map = Array.prototype.map | |
var reduce = Array.prototype.reduce | |
var slice = Array.prototype.slice | |
var slicer = isNative(slice) | |
? function nativeSlice (array, begin, end) { | |
return slice.call(array, begin, end) | |
} | |
: function slice (array, start, end) { | |
var l = array.length | |
if (start < 0) start = Math.max(0, l + start) | |
var size = (end < 0 ? l + end : Math.min(end, l)) - start | |
if (size <= 1) return [] | |
var sliced = new Array(size) | |
for (var i = size; i > start; i--) sliced[i] = array[i] | |
return sliced | |
} | |
function isNative (method) { | |
return method && typeof method === 'function' && regexpIsNative.test(toSource(method)) | |
} | |
var slapdash = { | |
bind: function bind (method, context) { | |
var args = slapdash.slice(arguments, 2) | |
return function boundFunction () { | |
return method.apply(context, args.concat(slapdash.slice(arguments))) | |
} | |
}, | |
each: isNative(forEach) | |
? function nativeEach (array, callback, context) { | |
return forEach.call(array, callback, context) | |
} | |
: function each (array, callback, context) { | |
var l = array.length | |
for (var i = 0; i < l; i++) callback.call(context, array[i], i, array) | |
}, | |
extend: isNative(assign) | |
? assign | |
: function assign (target) { | |
var l = arguments.length | |
for (var i = 1; i < l; i++) { | |
var source = arguments[i] | |
for (var j in source) if (source.hasOwnProperty(j)) target[j] = source[j] | |
} | |
return target | |
}, | |
filter: isNative(filter) | |
? function nativeFilter (array, callback, context) { | |
return filter.call(array, callback, context) | |
} | |
: function filter (array, callback, context) { | |
var l = array.length | |
var output = [] | |
for (var i = 0; i < l; i++) { | |
if (callback.call(context, array[i], i, array)) output.push(array[i]) | |
} | |
return output | |
}, | |
find: isNative(find) | |
? function nativeFind (array, callback, context) { | |
return find.call(array, callback, context) | |
} | |
: function find (array, callback, context) { | |
var l = array.length | |
for (var i = 0; i < l; i++) { | |
if (callback.call(context, array[i], i, array)) return array[i] | |
} | |
}, | |
indexOf: isNative(indexOf) | |
? function nativeIndexOf (array, item) { | |
return indexOf.call(array, item) | |
} | |
: function indexOf (array, item) { | |
var l = array.length | |
for (var i = 0; i < l; i++) { | |
if (array[i] === item) return i | |
} | |
return -1 | |
}, | |
invoke: function invoke (array, methodName) { | |
var args = slapdash.slice(arguments, 2) | |
return slapdash.map(array, function invokeMapper (value) { | |
return value[methodName].apply(value, args) | |
}) | |
}, | |
isMatch: function isMatch (obj, match) { | |
return slapdash.matches(match)(obj) | |
}, | |
keys: isNative(keys) | |
? keys | |
: function keys (object) { | |
var keys = [] | |
for (var key in object) { | |
if (object.hasOwnProperty(key)) keys.push(key) | |
} | |
return keys | |
}, | |
map: isNative(map) | |
? function nativeMap (array, callback, context) { | |
return map.call(array, callback, context) | |
} | |
: function map (array, callback, context) { | |
var l = array.length | |
var output = new Array(l) | |
for (var i = 0; i < l; i++) { | |
output[i] = callback.call(context, array[i], i, array) | |
} | |
return output | |
}, | |
matches: function matches (match) { | |
return function (obj) { | |
for (var i in match) { | |
if (match.hasOwnProperty(i) && obj[i] !== match[i]) { | |
return false | |
} | |
} | |
return true | |
} | |
}, | |
objectEach: function (object, callback, context) { | |
return slapdash.each(slapdash.keys(object), function (key) { | |
return callback.call(context, object[key], key, object) | |
}, context) | |
}, | |
objectMap: function objectMap (object, callback, context) { | |
var result = {} | |
for (var key in object) { | |
if (object.hasOwnProperty(key)) { | |
result[key] = callback.call(context, object[key], key, object) | |
} | |
} | |
return result | |
}, | |
objectReduce: function objectReduce (object, callback, initialValue) { | |
var output = initialValue | |
for (var i in object) { | |
if (object.hasOwnProperty(i)) output = callback(output, object[i], i, object) | |
} | |
return output | |
}, | |
pluck: function pluck (array, key) { | |
var l = array.length | |
var values = [] | |
for (var i = 0; i < l; i++) if (array[i]) values[i] = array[i][key] | |
return values | |
}, | |
reduce: isNative(reduce) | |
? function nativeReduce (array, callback, initialValue) { | |
return reduce.call(array, callback, initialValue) | |
} | |
: function reduce (array, callback, initialValue) { | |
var output = initialValue | |
var l = array.length | |
for (var i = 0; i < l; i++) output = callback(output, array[i], i, array) | |
return output | |
}, | |
slice: function slice (array, begin, end) { | |
begin = begin || 0 | |
end = typeof end === 'number' ? end : array.length | |
return slicer(array, begin, end) | |
}, | |
name: 'slapdash', | |
version: '0.4.1' | |
} | |
slapdash.objectMap.asArray = function objectMapAsArray (object, callback, context) { | |
return slapdash.map(slapdash.keys(object), function (key) { | |
return callback.call(context, object[key], key, object) | |
}, context) | |
} | |
module.exports = slapdash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment