Node Extensions
Extend Node's global
object. No dependencies except semver.
Usage
npm install --save node-extensions
This package attachs APIs to global
object (like es5-shim), so what you need to do is just require it once in your project's main file, e.g. index.js
or app.js
. That's all.
Recommend to require node extensions on the very top of file.
var obj = 0: 'Hello' 1: 'Node' length: 2 console// 'Hello Node' var fruits = 'Apple' 'Orange' if fruitsisEmpty === false // do somethinge 'the great gatsby' // 'the Great Gatsby'
Browser
Please use module bundler like Browserify or Webpack:
require('node-extensions/browser')
APIs
Object
Methods
assign
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign
Object// { type: 'node', name: 'node-extensions' }
hasProps
let obj = foo: bar: baz: 'baz' Object // trueObject // false
isObject
Object === trueObject === falseObject === falseObject === falseObject === false
isNull
Object === trueObject === false
isUndefined
Object === trueObject === false
merge
Deep version of Object#assign()
.
Object// { project: { type: 'node', name: 'node-extensions' } }
pick
Creates a new object that only contain specific keys.
let obj = foo: 'foo' bar: 'bar' baz: 'baz' ObjectObject// { foo: 'foo', bar: 'bar' } ObjectObject// { bar: 'bar', baz: 'baz' }
props
let obj = foo: bar: baz: 'baz' Object // 'baz'Object // undefined
Array
Properties
first
1 2first === 1first === undefinednullfirst === null
isEmpty
isEmpty === true1isEmpty === false isEmpty === truenullisEmpty === false
last
1 2last === 2last === undefinednulllast === null
Methods
compact
compact()
Like Ruby's Array#compact(), creates a new array that all undefined and null elements removed, or a new array with all elements that pass the test implemented by the provided function.
compact(mapfn[, thisArg])
alias of Array#filter()
.
1 null 2compact // [1, 2] 'foo' 'bar' 1 2compact { return typeof item === 'string'} // ['foo', 'bar']
collect(callback, thisArg)
1 undefined null 2 // [1, null, 2] 'foo' 'bar' 1 2 // ['foobaz', 'barbaz']
equalTo
equalTo(target, mapfn, thisArg)
1 2 === true'a' 1 true === true 2014 0 1 === false 2014 0 1 === true
includes
1 2 3 === true// Array#contains is an alias of Array#includes1 2 3 === true
insert
Array
var arr = 1 2 3arr // [1, 2, 3, 5]arr // [1, 2, 3, 4, 5]
find & findIndex
1 2 3 // 2 1 2 3 // 1
flatten
1 2 3 4 0: 5 length: 1 // [1, 2, 3, 4, 5]
from
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
For now, only support array and array-like object.
Array// [ 'a', 'b' ] Array// [ 'a', 'b', 'c' ] Array// [ 'a', 'b', 'c' ]
isArrayLike
Array === trueArray === trueArray === true Array === falseArray === falseArray === false var b = trueblength = 4Array === false
isEmpty
Array === trueArray === true
Iterators
forEach
, map
, every
, some
, filter
, reduce
, reduceRight
, indexOf
, lastIndexOf
and 'slice' are overrided for performance concerned.
The new methods are faster than built-in methods. By overriding the built-in methods, the general performances of using built-in methods should be improved. More details: benchmarks.
array 1 2 3// 3, 2, 1
of
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.of
Array; // [1]Array; // [1, 2, 3]Array; // [undefined]
pluck
Creates a collection for property values of path in all elements
var members = name: 'John' gender: 'male' name: 'Jane' gender: 'female' members// ['John', 'Jane']
remove
Array
var arr = 'foo' 'bar' 'foo'arr // 'foo'arr // ['foo', 'bar']arr // undefinedarr // ['foo', 'bar']
Boolean
Methods
isBoolean
isBoolean(target)
Boolean // trueBoolean // trueBoolean // trueBoolean // false
String
Properties
isEmpty
''isEmpty === true' 'isEmpty === true'str'isEmpty === false
Methods
caseCmp
Like Ruby's casecmp
.
'foo' // true'foo' // false'foo' // false
endsWith
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.startswith
'Hello' === true'Hello' === false'Hello' === false'Hello' === true
includes
http://people.mozilla.org//~jorendorff/es6-draft.html#sec-string.prototype.includes
'str' === true'str' === false'substr' === false// String#contains availables as an alias of String#includes'str' === true
isEmpty
String === trueString === true
isString
String === trueString === trueString === false
interpolate
'Good morning, ${title} #{lastname}.''Good morning, ${0} #{2}.''Good morning, ${0} #{2}.'// all outputs 'Good morning, Mr. Smith.'
You can use $
and #
for defining parameters.
Aliases: String#tpl()
& String#template()
padStart / padEnd
'foo' // barbarbfoo'foo' // foobarbarb
repeat
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat
'*' === '*****'
startsWith
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.startswith
'Hello' === true'Hello' === false'Hello' === false'Hello' === true
toCamelCase
http://stackoverflow.com/a/10425344
'hello world' === 'helloWorld''hello----world' === 'helloWorld''hello___world' === 'helloWorld''hello_world' === 'helloWorld''hello.world' === 'hello.world''hello.nodejs world' === 'hello.nodejsWorld' // Make first letter camel cased'hello world' === 'HelloWorld''hello----world' === 'HelloWorld''hello___world' === 'HelloWorld''hello_world' === 'HelloWorld''hello.world' === 'Hello.world''hello.nodejs world' === 'Hello.nodejsWorld'
toCapitalCase
'hello world' === 'Hello World''hello-world' === 'Hello-world''the great gatsby' === 'the Great Gatsby'
Note: I'm not a native speaker of English, use it at your own risk. Any contributions would be appreciated.
Function
Methods
isCallable
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-iscallable
{}Function === true var obj = {} Function === true Function === true
isFunction
Function === trueFunction === false
Symbol is not supported.
Number
Methods
isFloat
Number === trueNumber === true Number === falseNumber === falseNumber === falseNumber === falseNumber === falseNumber === falseNumber === falseNumber === false
isInteger
http://www.ecma-international.org/ecma-262/6.0/#sec-number.isinteger
Number === trueNumber === trueNumber === trueNumber === true Number === falseNumber === falseNumber === falseNumber === falseNumber === false
Notes
Overriding Properties
Due to the limitation of Object.defineProperty
, you have to use it when overriding properties:
var s = ''sisEmpty = 'hello'sisEmpty // true Object sisEmpty // 'hello'