utils-igor
v2.0.5
Published
Utils for nodeJs
Downloads
615
Maintainers
Readme
utils-igor
Installation
npm install utils-igor
### Example
package.json
```json
{
"name": "test_util",
"version": "0.0.1",
"description": "",
"main": "index.js",
"license": "UNLICENSED",
"dependencies" : {
"express" : "*",
"async": "1.4.2",
"utils-igor" : "*"
}
}
index.js
/**
* Created by igor on 30.05.16.
*/
/* globals process, require */
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.get('/', function (req, res) {
// include all blocks
var utils = require('utils-igor')();
var arr = [2,3,4,4,4,4,5];
var obj = {};
res.send( 200,{
objEmpty : utils.obj.isEmpty(obj),
isFunction : utils.type.isFn(obj),
arr : utils.arr.unique(arr)
});
});
app.listen(port, function () {
console.log('Example app listening on port '+ port + '!');
});
Result:
{"objEmpty":true,"isFunction":false,"arr":[2,3,4,5]}
To include one block change
- var utils = require('utils-igor')();
+ var utils = require('utils-igor')('obj');
To include more than one block change
- var utils = require('utils-igor')();
+ var utils = require('utils-igor')(['obj', 'arr']);
Modules
arr
Function for array
- arr
- .mvVal
- .check(arr)
- .sortNumber(a, b) ⇒ number
- .share(arr1, arr2) ⇒ Array
- .diff(arr1, arr2) ⇒ Array
- .unique(arr)
- .sum() ⇒ Number
- .avg(a) ⇒ number
- .urls(arr) ⇒ Array
arr.mvVal
Kind: static class of arr
new exports.mvVal(arr, value)
Delete all elements whith specified value
| Param | Type | Description | | --- | --- | --- | | arr | Array | массив для очистки | | value | * | значение |
arr.check(arr)
Check parameter be array. if not return empty array
Kind: static method of arr
| Param | Type | | --- | --- | | arr | * |
arr.sortNumber(a, b) ⇒ number
By default the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts e.g. [11, 1, 2].sort(utils.sortNumber);
Kind: static method of arr
| Param | Type | | --- | --- | | a | Number | | b | Number |
arr.share(arr1, arr2) ⇒ Array
Return share part of the array
Kind: static method of arr
| Param | | --- | | arr1 | | arr2 |
arr.diff(arr1, arr2) ⇒ Array
Return different part of the array
Kind: static method of arr
| Param | Type | | --- | --- | | arr1 | Array | | arr2 | Array |
arr.unique(arr)
Clear repeat values
Kind: static method of arr
| Param | Type | | --- | --- | | arr | Array |
arr.sum() ⇒ Number
Sum of array elements
Kind: static method of arr
arr.avg(a) ⇒ number
Return average elements array
Kind: static method of arr
| Param | Type | | --- | --- | | a | Array |
arr.urls(arr) ⇒ Array
Return array. When element is url collect from specified array
Kind: static method of arr
| Param | Type | | --- | --- | | arr | Array |
date
Function for date
date.ts : Object
Time constant in milliseconds
Kind: static property of date
date.time(ts)
Get time string
Kind: static method of date
| Param | Type | | --- | --- | | ts | Number | null | undefined |
Example
'11:40:46'
date.date(shiftDays, ts, format) ⇒ string
Get date string
Kind: static method of date
| Param | Type | | --- | --- | | shiftDays | Number | null | | ts | Number | null | undefined | | format | string | null | undefined |
Example
date() => '2014-11-30'
Example
date(null, null, 'd-m-y') => '30-11-2014'
date.dateTime(shiftDays, ts, format)
- Get date time string
Kind: static method of date
| Param | Type | | --- | --- | | shiftDays | Number | null | undefined | | ts | Number | null | undefined | | format | String | null | undefined |
Example
'2014-11-30 11:40:46'
Example
dateTime() this work
date.tsToSec(ts, up)
unix-time to seconds
Kind: static method of date
| Param | Type | | --- | --- | | ts | Number | null | undefined | | up | boolean | null |
Example
1417323330
date.tsToMin(ts, up)
unix-time to minutes
Kind: static method of date
| Param | Type | | --- | --- | | ts | Number | null | undefined | | up | boolean | null |
Example
23622056
date.secToTs(sec)
seconds to unix-time
Kind: static method of date
| Param | Type | | --- | --- | | sec | Number |
Example
1417323330
date.minToTs(min)
minutes to unix-time
Kind: static method of date
| Param | Type | | --- | --- | | min | Number |
Example
23622056
date~unix(ts)
Return date (now or by unixTime).
Kind: inner method of date
| Param | Type | | --- | --- | | ts | Number | null | undefined |
date~round(number, up)
Be round up or down
Kind: inner method of date
| Param | Type | | --- | --- | | number | Number | | up | boolean | null |
obj
Function for object
- obj
- static
- .arrToObjByKey(arr, prop, fnIterProp) ⇒ Object
- .keysChange(obj, keys) ⇒ Object
- .sort(obj, down) ⇒ Object
- .propInc(obj, prop, byVal)
- .beInObj(ob, prop, def)
- .pathCreate(obj, path, def)
- .isPathExist(obj, path) ⇒ Boolean
- .pathVal(obj, path) ⇒ *
- .pathMv(obj, path) ⇒ *
- .urlParams(obj) ⇒ string
- .ext(obj, add) ⇒ *
- .getPropToArr(arObj, prop) ⇒ Array
- .propToArr(obj) ⇒ Array
- .each(obj, fn, fnSort)
- .isEmpty(obj) ⇒ boolean
- .beRound(ob, prop, round)
- inner
- ~isObj(obj) ⇒ boolean
- ~isSet(v)
- static
obj.arrToObjByKey(arr, prop, fnIterProp) ⇒ Object
Return object. When keys is specified property, value is object from array objects
Kind: static method of obj
| Param | Type | | --- | --- | | arr | Array | | prop | String | | fnIterProp | function |
obj.keysChange(obj, keys) ⇒ Object
Get properties from obj by keys
Kind: static method of obj
| Param | Type | Description | | --- | --- | --- | | obj | object | | | keys | * | [{keyOld : keyNew,...}] |
obj.sort(obj, down) ⇒ Object
Return new object when keys be sort
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | down | Boolean |
obj.propInc(obj, prop, byVal)
Increment object property to the specified value
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | prop | String | | byVal | Number |
obj.beInObj(ob, prop, def)
Check is set object. If not, it creates it with the specified value
Kind: static method of obj
| Param | Type | | --- | --- | | ob | Object | | prop | String | | def | Object |
obj.pathCreate(obj, path, def)
Create path in object
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | Array | String | | path | Array | String | null | undefined | | def | * |
obj.isPathExist(obj, path) ⇒ Boolean
Check exist path in object
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | path | * |
obj.pathVal(obj, path) ⇒ *
Get element by path in object
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | path | String |
obj.pathMv(obj, path) ⇒ *
Delete element by path in object
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | path | String |
obj.urlParams(obj) ⇒ string
Serialize object to url params
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object |
obj.ext(obj, add) ⇒ *
Properties in add object adding to obj or if properties not exits create him
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | add | Object |
obj.getPropToArr(arObj, prop) ⇒ Array
Get property from array objects
Kind: static method of obj
| Param | Type | | --- | --- | | arObj | Array | | prop | String |
obj.propToArr(obj) ⇒ Array
Collect object property to array
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object |
obj.each(obj, fn, fnSort)
ForEach for Object fn(key, val) and sort keys by fnSort
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object | | fn | function | | fnSort | function |
obj.isEmpty(obj) ⇒ boolean
if o is object return true else false
Kind: static method of obj
| Param | Type | | --- | --- | | obj | Object |
obj.beRound(ob, prop, round)
If the object property exists, its rounded
Kind: static method of obj
| Param | Type | | --- | --- | | ob | Object | | prop | String | | round | Number |
obj~isObj(obj) ⇒ boolean
Check is object
Kind: inner method of obj
| Param | Type | | --- | --- | | obj | Object |
obj~isSet(v)
Check v be no undefined or null
Kind: inner method of obj
| Param | Type | | --- | --- | | v | * |
str
Function for string
- str
- .base64(str, decode) ⇒ string
- .salt(len) ⇒ String
- .hash(str, salt, secret, method) ⇒ *
- .up1stChar(s) ⇒ string
- .regexpEscape(text)
- .fullReplace(str, find, replace)
- .htmlEscape(str, maxLength) ⇒ *
- .oneSpace(str) ⇒ String
- .removeSpecSymbols(str) ⇒ String
- .makeKey(accept, n) ⇒ string
- .decodeURIUniversal(str) ⇒ String
- .boolString(vl) ⇒ Boolean
str.base64(str, decode) ⇒ string
Return string encode/decode in base64
Kind: static method of str
| Param | Type | | --- | --- | | str | | | decode | boolean |
str.salt(len) ⇒ String
Return salt
Kind: static method of str
Returns: String - salt
| Param | Type | | --- | --- | | len | Number | null | undefined |
str.hash(str, salt, secret, method) ⇒ *
Return string hash
Kind: static method of str
| Param | Type | | --- | --- | | str | String | | salt | String | null | undefined | | secret | String | null | undefined | | method | 'md5' | 'sha512' | 'sha256' | null | undefined |
str.up1stChar(s) ⇒ string
Return string, fist char in upper case.
Kind: static method of str
| Param | Type | | --- | --- | | s | string |
str.regexpEscape(text)
Escapes special characters for RegExp
Kind: static method of str
| Param | Type | | --- | --- | | text | String |
str.fullReplace(str, find, replace)
Replace all find word to replace word
Kind: static method of str
| Param | Type | | --- | --- | | str | String | | find | String | | replace | String |
str.htmlEscape(str, maxLength) ⇒ *
Escapes special characters for html and trim unnecessary
Kind: static method of str
| Param | Type | | --- | --- | | str | String | | maxLength | Number | null | undefined |
str.oneSpace(str) ⇒ String
Replace spaces to only one space
Kind: static method of str
Returns: String - Output string
| Param | Type | Description | | --- | --- | --- | | str | String | string |
str.removeSpecSymbols(str) ⇒ String
Replace all spacial symbols to space
Kind: static method of str
Returns: String - Output string
| Param | Type | Description | | --- | --- | --- | | str | String | string |
str.makeKey(accept, n) ⇒ string
It generates key specified length. Used accept symbols
Kind: static method of str
| Param | Type | | --- | --- | | accept | String | null | undefined | | n | Number | null | undefined |
str.decodeURIUniversal(str) ⇒ String
Decoding URI by all methods
Kind: static method of str
| Param | Type | | --- | --- | | str | String |
str.boolString(vl) ⇒ Boolean
Return value after convert string boolean to boolean. if vl === 'true' then vl = true. Other vl = false
Kind: static method of str
| Param | Type | | --- | --- | | vl | String |
type
Working with types
- type
- .isString(s) ⇒ boolean
- .isSet(v) ⇒ boolean
- .noop()
- .isFn(f) ⇒ boolean
- .isObj(o)
- .beFn(fn) ⇒ *
- .cloneVar(v) ⇒ string
- .isNm(n)
type.isString(s) ⇒ boolean
Check s is string
Kind: static method of type
| Param | Type | | --- | --- | | s | Mixed |
type.isSet(v) ⇒ boolean
Check v is not null or undefined
Kind: static method of type
| Param | Type | | --- | --- | | v | Mixed |
type.noop()
Empty function
Kind: static method of type
type.isFn(f) ⇒ boolean
if f is function return true else false
Kind: static method of type
| Param | Type | | --- | --- | | f | function |
type.isObj(o)
Check parameter is object
Kind: static method of type
| Param | Type | | --- | --- | | o | Mixed |
type.beFn(fn) ⇒ *
if fn is not function change to empty function
Kind: static method of type
| Param | Type | | --- | --- | | fn | function |
type.cloneVar(v) ⇒ string
Return new instance variable v
Kind: static method of type
| Param | Type | | --- | --- | | v | Mixed |
type.isNm(n)
Check this value is number
Kind: static method of type
| Param | Type | | --- | --- | | n | Number |
Version
2.0.0 What is new ? - Used "use strict" mode. - Used minification js files. - Attach unit test - Maximum use ESMA6 within Node 4.x.x - Change : Remove function obj.clone him be in type.cloneVar Remove function date.showTime add parameter format in date.date Add new function arr.check And etc... 2.0.2 Adding time constant in date module 2.0.3 Correct function arr.mvVal
People
Author and developer is Igor Stcherbina
License
MIT
Free Software