[go: nahoru, domu]

nonstandard

1.1.1 • Public • Published

Readme

NPM

Build Status Dependencies npm version Code Climate Test Coverage bitHound Overalll Score

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

Log value to console and pass value First value was passed next.

console.pipe(returnValueany, logValue1any, logValue2any, logValueNany) : 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(valueany, ...valuesany[]) : any;
console.info.pipe(valueany, ...valuesany[]) : any;
console.warn.pipe(valueany, ...valuesany[]) : any;
console.error.pipe(valueany, ...valuesany[]) : 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(targetarray) : boolean;

Install:

require('nonstandard/src/array/empty').Install();
Array.empty([]) // true
Array.empty([1, 2, 3]) // false

Array.present

Check if array has some elements

Array.present(targetarray) : boolean;

Install:

require('nonstandard/src/array/present').Install();
Array.present([]) // false
Array.present([1, 2, 3]) // true

Array.prototype.first

First element in array

[].first

Install:

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

[].second

Install:

require('nonstandard/src/array/properties').Install();
var arr = [1, 8, 10];
 
arr.second // 8
 
[].second // undefined
 
[].second = 2 // equals:  [][1] = 2

Array.prototype.last

Latest element in array

[].last

Install:

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 changed

Array.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[]) : boolean

Install:

require('nonstandard/src/array/contains').Install();
var arr = ["a", "b", "c"];
 
arr.contains(['a', 'b']); // true
 
arr.contains('c'); // true
 
arr.contains('d'); // false

Array.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(targetobject) : 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 memory

Object.empty

Check if object has not keys

Object.empty(targetobject) : boolean;

Install:

require('nonstandard/src/object/empty').Install();
Object.empty({}) // true
Object.empty({ a: 2 }) // false
Object.empty(window) // false

Object.present

Check if object has keys

Object.present(targetobject) : boolean;

Install:

require('nonstandard/src/object/present').Install();
Object.present({}) // false
Object.present({ a: 2 }) // true
Object.present(window) // true

Number.range

Create array of numbers

Number.range(minnumber, maxnumber, stepnumber = 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(lengthstring = 10) : string;

Install:

require('nonstandard/src/string/random').Install();
String.random() // "778fx0hmc0"
String.random(5) // "u5ojh"
String.random(64) // "ww5z8kar8m6l4ichouw221n307xiec1wt6584qf4bib2rbsa4b379hoblsd42h3e"

Package Sidebar

Install

npm i nonstandard

Weekly Downloads

3

Version

1.1.1

License

MIT

Last publish

Collaborators

  • lestad