nonstandard
v1.1.1
Published
Non standard JavaScript features
Maintainers
Readme
Readme
Some non standard JavaScript features.
from Node.js > 0.12.0
Usage
require('nonstandard'); // Install all features
// install specified function
require('nonstandard/src/array/contains').Install();
[1, 2, 3].contains(2); // true
// Install some module
require('nonstandard/src/array').Install();
require('nonstandard/src/console').Install();
require('nonstandard/src/number').Install();
require('nonstandard/src/object').Install();
require('nonstandard/src/string').Install();Contents
- console.pipe
- console.[log, info, warn, error].pipe
- Array.empty
- Array.present
- Array.prototype.first
- Array.prototype.second
- Array.prototype.last
- Array.prototype.clean
- Array.prototype.contains
- Array.prototype.clone
- Object.clone
- Object.empty
- Object.present
- Number.range
- Number.prototype.times
- String.random
console.pipe
Log value to console and pass value First value was passed next.
console.pipe(returnValue: any, logValue1: any, logValue2: any, logValueN: any) : any;Install:
require('nonstandard/src/console').Install();// write `123` to console with `console.log`
var a = console.pipe(123);
// write:
// hello world
// hello
//
console.log(console.pipe('hello', 'world'));console.[log, info, warn, error].pipe
console.log.pipe(value: any, ...values: any[]) : any;
console.info.pipe(value: any, ...values: any[]) : any;
console.warn.pipe(value: any, ...values: any[]) : any;
console.error.pipe(value: any, ...values: any[]) : any;Install:
require('nonstandard/src/console').Install();executeFunction(console.log.pipe({ some: 'data' }, "that string has been logged only")); // value was shown in console, and passed to function `executeFunction`
function actionLogin(user) {
return login(user.credentials)
.then(function(resp) {
return console.info.pipe(resp.data);
})
.catch(function(error) {
throw console.error.pipe(error); // in promise throwns was catched by next then/catch pair
});
}Array.empty
Check if array has not elements
Array.empty(target: array) : boolean;Install:
require('nonstandard/src/array/empty').Install();Array.empty([]) // true
Array.empty([1, 2, 3]) // falseArray.present
Check if array has some elements
Array.present(target: array) : boolean;Install:
require('nonstandard/src/array/present').Install();Array.present([]) // false
Array.present([1, 2, 3]) // trueArray.prototype.first
First element in array
[].firstInstall:
require('nonstandard/src/array/properties').Install();var arr = [5, 9, 14];
arr.first // 5
arr.first = 2;
arr // [2, 9, 14]Array.prototype.second
Second element in array
[].secondInstall:
require('nonstandard/src/array/properties').Install();var arr = [1, 8, 10];
arr.second // 8
[].second // undefined
[].second = 2 // equals: [][1] = 2Array.prototype.last
Latest element in array
[].lastInstall:
require('nonstandard/src/array/properties').Install();var arr = [12, 34, 56, 78, 90];
arr.last // 90
arr.last == arr[arr.length - 1] // true
[].last = 1; // nothing changedArray.prototype.clean
Clear array
[].clean()Install:
require('nonstandard/src/array/clean').Install();var arr = [1, 2, 3, 45, 67];
arr.clean();
arr // now `[]`If as argument function passed, .clean call it on every value and clean if callback return not false, 0, null or undefined
var arr = [1, 2, 3, 4, 5, 6];
arr.clean(function(e){
return e % 2;
});
arr; // [2, 4, 6]Array.prototype.contains
Check elements exists in array
[].contains(any[]) : booleanInstall:
require('nonstandard/src/array/contains').Install();var arr = ["a", "b", "c"];
arr.contains(['a', 'b']); // true
arr.contains('c'); // true
arr.contains('d'); // falseArray.prototype.clone
Full clone array.
[].clone() : any[]Install:
require('nonstandard/src/array/clone').Install();var arr = [1, 5].clone();
// its simple
arr // [1, 5]Object.clone
Deep clone object
Object.clone(target: object) : object;Install:
require('nonstandard/src/object/clone').Install();var oob = { name: "Foo", test: "bar", cool: { yeah: true } };
var wob = Object.clone(oob);
// Object `wob` is full copy of `oob` without links in memoryObject.empty
Check if object has not keys
Object.empty(target: object) : boolean;Install:
require('nonstandard/src/object/empty').Install();Object.empty({}) // true
Object.empty({ a: 2 }) // false
Object.empty(window) // falseObject.present
Check if object has keys
Object.present(target: object) : boolean;Install:
require('nonstandard/src/object/present').Install();Object.present({}) // false
Object.present({ a: 2 }) // true
Object.present(window) // trueNumber.range
Create array of numbers
Number.range(min: number, max: number, step: number = 1) : number[];Install:
require('nonstandard/src/number/present').Install();console.log(Number.range(1, 10))
/*
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
console.log(Number.range(2, 8, 2))
/*
[2, 4, 6, 8]
*/Number.prototype.times
Run callback n times
(5).times(function(index: number) { return index + 1; }) : any[]Install:
require('nonstandard/src/number/times').Install();var result = (5).times(function(index) { return ++index * 2; });
result // [ 2, 4, 6, 8, 10 ]String.random
Generate random string with custom length
String.random(length: string = 10) : string;Install:
require('nonstandard/src/string/random').Install();String.random() // "778fx0hmc0"
String.random(5) // "u5ojh"
String.random(64) // "ww5z8kar8m6l4ichouw221n307xiec1wt6584qf4bib2rbsa4b379hoblsd42h3e"
