Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fd424f6
Showing
7 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# core-utils # | ||
|
||
Utility library for everyday javasciprt. | ||
|
||
## Install ## | ||
|
||
npm install core-utils | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
var _call = Function.prototype.call | ||
|
||
var forEach = _call.bind(Array.prototype.forEach) | ||
exports.forEach = forEach | ||
|
||
var reduce = _call.bind(Array.prototype.reduce) | ||
exports.reduce = reduce | ||
|
||
var map = _call.bind(Array.prototype.map) | ||
exports.map = map | ||
|
||
var every = _call.bind(Array.prototype.every) | ||
exports.every = every | ||
|
||
var some = _call.bind(Array.prototype.some) | ||
exports.some = some | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
if (!("bind" in Function.prototype)) | ||
Function.prototype.bind = function bind() { | ||
var bound = this | ||
var boundArgs = Array.prototype.slice.call(arguments) | ||
return function anonymous() { | ||
var self, args | ||
args = boundArgs.concat(Array.prototype.slice.call(arguments)) | ||
if (this instanceof anonymous) { | ||
self = Object.create(bound.prototype) | ||
return bound.apply(self, args) || self | ||
} else { | ||
return bound.call.apply(bound, args) | ||
} | ||
} | ||
} | ||
|
||
var _call = Function.prototype.call | ||
var _apply = Function.prototype.apply | ||
|
||
/** | ||
* Calls given `callee` with a given `scope` as `this` pseudo-variable | ||
* and `args` as an arguments. | ||
* @param {Function} callee | ||
* Function to call | ||
* @param {Object} scope | ||
* `this` pseudo-variable in the callee's lexical scope. | ||
* @param {[]} args | ||
* arguments passed to the callee | ||
*/ | ||
var apply = _call.bind(_apply) | ||
exports.apply = apply | ||
|
||
/** | ||
* Calls given `callee` and passes given `scope` argument as `this` | ||
* pseudo-variable and all the rest arguments are passed as an | ||
* arguments. | ||
* @param {Function} callee | ||
* Function to call | ||
* @param {Object} scope | ||
* `this` pseudo-variable in the callee's lexical scope. | ||
* @params | ||
* All the rest arguments are passed to the callee. | ||
*/ | ||
var call = _call.bind(_call) | ||
exports.call = call | ||
|
||
// Binds `callee` to the given `scope` as `this` pseudo-variable | ||
// and rest arguments. | ||
var bind = _call.bind(Function.prototype.bind) | ||
exports.bind = bind | ||
|
||
var owns = _call.bind(Object.prototype.hasOwnProperty) | ||
exports.owns = owns | ||
|
||
function has(object, name) { | ||
return name in object | ||
} | ||
exports.has = has | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict' | ||
|
||
var type = require("./type") | ||
var array = require("./array") | ||
var utils = require("./core-utils") | ||
|
||
var _call = Function.prototype.call | ||
|
||
|
||
var get = function get(object, name) { | ||
return object[name] | ||
} | ||
exports.get = get | ||
_get = _call.bind(get) | ||
|
||
var set = function set(object, name, value) { | ||
return object[name] = value | ||
} | ||
exports.set = set | ||
_set = _call.bind(set) | ||
|
||
var remove = function remove(object) { | ||
return delete object[name] | ||
} | ||
exports.remove = remove | ||
_remove = _call.bind(remove) | ||
|
||
function keyValue(object, name) { | ||
return [name, object[name]] | ||
} | ||
_keyValue = _call.bind(keyValue) | ||
|
||
var keys = Object.keys | ||
exports.keys = keys | ||
|
||
var values = function values(object) { | ||
keys(object).map(_get, object) | ||
} | ||
exports.values = values | ||
|
||
var keyValues = function keyValues() { | ||
keys(object).map(_keyValue, object) | ||
} | ||
|
||
var defineProperty = Object.defineProperty | ||
exports.defineProperty = defineProperty | ||
|
||
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor | ||
exports.getOwnPropertyDescriptor = getOwnPropertyDescriptor | ||
|
||
var getOwnPropertyDescriptors = function getOwnPropertyDescriptors(object) { | ||
var map = {} | ||
keys(object).forEach(function(key) { | ||
map[key] = getOwnPropertyDescriptor(object, key) | ||
}) | ||
return map | ||
} | ||
|
||
var extend = function extend(target) { | ||
array.forEach(arguments, function(source) { | ||
if (source !== target) | ||
defineProperties(target, getOwnPropertyDescriptors(target)) | ||
}) | ||
return target | ||
} | ||
exports.extend = extend | ||
|
||
function merge(target) { | ||
array.forEach(arguments, function(source) { | ||
if (source !== target) { | ||
array.forEach(keys(source), function(key) { | ||
if (!utils.owns(target, key)) | ||
defineProperty(target, key, getOwnPropertyDescriptor(source, key)) | ||
else if (type.isObject(target[key]) && type.isObject(source[key])) | ||
merge(target[key], source[key]) | ||
}) | ||
} | ||
}) | ||
return target | ||
} | ||
exports.merge = merge | ||
|
||
function rename(object, renames, target) { | ||
target = target || {} | ||
var properties = getOwnPropertyDescriptors(object) | ||
keys(properties).forEach(function (name) { | ||
defineProperty(target, renames[name] || name, properties[name]) | ||
}) | ||
return target | ||
} | ||
exports.rename = rename |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// vim:ts=2:sts=2:sw=2: | ||
|
||
var isArray = Array.isArray.bind(Array); | ||
exports.isArray = isArray; | ||
|
||
function isAtom(thing) { | ||
return !isFunction(thing) && !isObject(thing) | ||
} | ||
exports.isAtom = isAtom; | ||
|
||
function isFunction(thing) { | ||
return 'function' === typeof thing | ||
} | ||
exports.isFunction = isFunction; | ||
|
||
function isObject(thing) { | ||
return 'object' === typeof thing && null !== thing | ||
} | ||
exports.isObject = isObject; | ||
|
||
function isPlainObject(thing) { | ||
return isObject(thing) && | ||
null === Object.getPrototypeOf(Object.getPrototypeOf(thing)); | ||
} | ||
exports.isPlainObject = isPlainObject; | ||
|
||
function isJSON(value, visited) { | ||
// Adding value to array of visited values. | ||
(visited || (visited = [])).push(value); | ||
// If `value` is an atom return `true` cause it's valid JSON. | ||
return isAtom(value) || | ||
// If `value` is an array of JSON values that has not been visited | ||
// yet. | ||
(isArray(value) && value.every(function(element) { | ||
return isJSON(element, visited); | ||
})) || | ||
// If `value` is a plain object containing properties with a JSON | ||
// values it's a valid JSON. | ||
(isPlainObject(value) && Object.keys(value).every(function(key) { | ||
var $ = Object.getOwnPropertyDescriptor(value, key); | ||
// Check every proprety of a plain object to verify that | ||
// it's neither getter nor setter, but a JSON value, that | ||
// has not been visited yet. | ||
return ((!isObject($.value) || !~visited.indexOf($.value)) && | ||
!('get' in $) && !('set' in $) && | ||
isJSON($.value, visited)); | ||
})); | ||
} | ||
exports.isJSON = isJSON |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ "name": "core-utils" | ||
, "version": "0.0.1" | ||
, "description": "Utility library for everyday javasciprt." | ||
, "homepage": "http://github.com/Gozala/core-utils/" | ||
, "keywords": ["utils", "core"] | ||
, "contributors": | ||
[ "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)" ] | ||
, "repository": | ||
{ "type": "git" | ||
, "url": "git://github.com/Gozala/core-utils.git" | ||
} | ||
, "bugs": { "web": "http://github.com/Gozala/core-utils/issues/" } | ||
, "directories": | ||
{ "doc": "./docs" | ||
, "lib": "./lib" | ||
, "test": "./test" | ||
} | ||
, "dependencies": { "test": ">=0.0.10" } | ||
, "main": "./lib/core-utils.js" | ||
, "scripts": { "test": "node test/all.js" } | ||
, "engines": | ||
{ "node": ">=0.1.103" | ||
, "teleport": ">=0.2.0" | ||
} | ||
, "licenses" : | ||
[ { "type" : "MPL 1.1/LGPL 2.1/GPL 2.0" | ||
, "url" : "http://www.mozilla.org/MPL/" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
|
||
|
||
if (module == require.main) require('test').run(exports) |