vv-shared
v5.0.4
Published
many small general-purpose functions
Downloads
17
Readme
Install & Use
npm i vv-shared
const vvs = require('vv-shared')
console.log(vvs.toString(42))
Functions
Typedefs
isEmpty(object) ⇒ boolean
Check object for undefined, null, NaN
Kind: global function
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
isEmptyString(object) ⇒ boolean
isEmpty + .trim() + check len > 0
Kind: global function
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
isFunction(object) ⇒ boolean
Check object for function
Kind: global function
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
isGuid(object) ⇒ boolean
Check object for GUID
Kind: global function
Returns: boolean - always boolean (no undefined)
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
Example
console.log(require('vv-shared').isGuid(undefined)) // return false
console.log(require('vv-shared').isGuid(null)) // return false
console.log(require('vv-shared').isGuid('')) // return false
console.log(require('vv-shared').isGuid('A36E9853-7118-4CC2-B770-765FCF05A82B')) // return true
nz(object1, object2, [object3], [object4], [object5]) ⇒ any
Return first non-empty parameter
Kind: global function
| Param | Type | | --- | --- | | object1 | any | | object2 | any | | [object3] | any | | [object4] | any | | [object5] | any |
equal(object1, object2) ⇒ boolean
Equal two objects
Kind: global function
| Param | Type | | --- | --- | | object1 | any | | object2 | any |
duplicates(where_find_duplicates) ⇒ Array.<string>
Return array with duplicates items from string array (after trim and toLowerCase)
Kind: global function
| Param | Type | | --- | --- | | where_find_duplicates | Array.<string> |
toString(value, [default_value]) ⇒ string
Convert object to string
Kind: global function
Returns: string - string or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toString(undefined)) // return undefined
console.log(require('vv-shared').toString(undefined,'my default string')) // return 'my default string'
console.log(require('vv-shared').toString({a: 5},'my default string')) // return 'my default string'
console.log(require('vv-shared').toString([1,2,3],'my default string')) // return 'my default string'
console.log(require('vv-shared').toString('','my default string')) // return empty string
console.log(require('vv-shared').toString(new Date(),'my default string')) // return formatDate(..., 126)
console.log(require('vv-shared').toString(45,'my default string')) // return '45'
console.log(require('vv-shared').toString('hello','my default string')) // return 'hello'
toStringDeclension(value, phrase_one, phrase_two, phrase_few) ⇒ string
Add phrase to int
Kind: global function
| Param | Type | Description | | --- | --- | --- | | value | number | int for add phrase | | phrase_one | string | | | phrase_two | string | | | phrase_few | string | |
Example
console.log(require('vv-shared').toStringDeclension(5, 'найдена', 'найдено', 'найдены'))
toInt(value, [default_value]) ⇒ number
Convert object to integer
Kind: global function
Returns: number - integer or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toInt('abc')) // return undefined
console.log(require('vv-shared').toInt('abc','xyz')) // return undefined
console.log(require('vv-shared').toInt('77',42)) // return 77
console.log(require('vv-shared').toInt('-77',42)) // return -77
console.log(require('vv-shared').toInt('77.2',42)) // return 42
toFloat(value, [default_value]) ⇒ number
Convert object to float
Kind: global function
Returns: number - float or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toFloat('abc')) // return undefined
console.log(require('vv-shared').toFloat('abc','xyz')) // return undefined
console.log(require('vv-shared').toFloat('abc','-42.42')) // return -42.42
console.log(require('vv-shared').toFloat('77',42)) // return 77
console.log(require('vv-shared').toFloat('77.2',42)) // return 77.2
console.log(require('vv-shared').toFloat('-77.2',42)) // return -77.2
toBool(value, [default_value]) ⇒ boolean
Convert object to boolean
Kind: global function
Returns: boolean - boolean or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toBool(2)) // return undefined
console.log(require('vv-shared').toBool('abc')) // return undefined
console.log(require('vv-shared').toBool('abc','false')) // return false
console.log(require('vv-shared').toBool(0)) // return true
console.log(require('vv-shared').toBool(1)) // return true
console.log(require('vv-shared').toBool('TruE')) // return true
console.log(require('vv-shared').toBool('true')) // return true
console.log(require('vv-shared').toBool('1')) // return true
toGuid(value, [default_value]) ⇒ string
Convert object to guid
Kind: global function
Returns: string - guid or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toGuid(undefined)) // return undefined
console.log(require('vv-shared').toGuid('')) // return undefined
console.log(require('vv-shared').toGuid('A36E9853-7118-4CC2-B770-765FCF05A82B')) // return 'A36E9853-7118-4CC2-B770-765FCF05A82B'
toDate([value], [default_value]) ⇒ Date
Convert object to date (with current GMT)
Kind: global function
Returns: Date - date or undefined
| Param | Type | Description | | --- | --- | --- | | [value] | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toDate('2018-04-12T16:35:49')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.1')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.12')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.123')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.1234')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.12345')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.123456')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.1234567')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.12345678')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.123456789')) // return true date
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.123+03:00')) // return true date WITH CURRENT GMT (ignore +03 from example)
console.log(require('vv-shared').toDate('2018-04-12T16:35:49.123Z')) // return true date
console.log(require('vv-shared').toDate('2018-04-12')) // return true date
console.log(require('vv-shared').toDate('12.04.2018')) // return true date
console.log(require('vv-shared').toDate('12.04.2018 16:35')) // return true date
console.log(require('vv-shared').toDate('12.04.2018 16:35:49')) // return true date
console.log(require('vv-shared').toDate('12-04-2018')) // return true date
console.log(require('vv-shared').toDate('12-04-2018 16:35')) // return true date
console.log(require('vv-shared').toDate('12-04-2018 16:35:49')) // return true date
console.log(require('vv-shared').toDate('20180412')) // return true date
toDateWithoutTime(value, [default_value]) ⇒ Date
Convert object to date without time
Kind: global function
Returns: Date - date without time or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
// because this based on toDate() with add cut off time, get them examples and change toDate to toDateWithoutTime
toTime(value, [default_value]) ⇒ Date
Convert object to date 01.01.1900 with time from object
Kind: global function
Returns: Date - date 01.01.1900 with time from or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
// because this based on toDate() with change day,month,year to 01.01.1900, get them examples and change toDate to toTime
console.log(require('vv-shared').toTime('16:35')) // return true date
console.log(require('vv-shared').toTime('16:35.49')) // return true date
console.log(require('vv-shared').toTime('16:35.49.1')) // return true date
console.log(require('vv-shared').toTime('16:35.49.12')) // return true date
console.log(require('vv-shared').toTime('16:35.49.123')) // return true date
console.log(require('vv-shared').toTime('16:35.49.1234')) // return true date
console.log(require('vv-shared').toTime('16:35.49.12345')) // return true date
console.log(require('vv-shared').toTime('16:35.49.123456')) // return true date
console.log(require('vv-shared').toTime('16:35.49.1234567')) // return true date
console.log(require('vv-shared').toTime('16:35.49.12345678')) // return true date
console.log(require('vv-shared').toTime('16:35.49.123456789')) // return true date
toHex(value, [default_value]) ⇒ string
convert Buffer or Array[int] to hex
Kind: global function
| Param | Type | | --- | --- | | value | Buffer | Array.<Number> | | [default_value] | Buffer |
toIp(value, [default_value]) ⇒ string
Contert object to string with IP format
Kind: global function
Returns: string - string with IP format or undefined
| Param | Type | Description | | --- | --- | --- | | value | any | object for convert | | [default_value] | any | default value |
Example
console.log(require('vv-shared').toIp('LOCALHOST')) // return 'localhost'
console.log(require('vv-shared').toIp('localhost')) // return 'localhost'
console.log(require('vv-shared').toIp('192.168.1.2')) // return '192.168.1.2'
console.log(require('vv-shared').toIp('abc','192.168.1.2')) // return '192.168.1.2'
console.log(require('vv-shared').toIp('abc')) // return undefined
toArray(value, [type]) ⇒ Array.<Object>
Convert object to array, used for params like {string|string[]}
Kind: global function
| Param | Type | Description | | --- | --- | --- | | value | any | Array.<any> | object for convert | | [type] | 'string' | 'int' | 'float' | 'bool' | 'guid' | 'date' | |
toCharArray(char, count) ⇒ string
Returns a string filled with the specified character
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | char | string | char or string for fill | | count | number | count fill |
Example
console.log(require('vv-shared').toCharArray('abc',2)) // return 'abcabc'
console.log(require('vv-shared').toCharArray(undefined,2)) // return empty string
console.log(require('vv-shared').toCharArray('abc',undefined)) // return empty string
toErrorMessage(error, [prefix], [replaces], [sourсe]) ⇒ string
Returns a nice formatted error message - wrapper for format()
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | error | any | | | [prefix] | string | prefix text for error message | | [replaces] | any | substitutions for prefix string | | [sourсe] | 'stack' | 'message' | preferred sourсe for extraction text message, default = 'stack' |
Example
try {throw new Error('ops')} catch (error) {throw require('vv-shared').toErrorMessage(error, 'in myFunction({0})','value')}
toHtml(value) ⇒ string
Return string for inject in html
Kind: global function
| Param | Type | | --- | --- | | value | string |
roundFload(value, digits) ⇒ number
True round
Kind: global function
| Param | Type | | --- | --- | | value | number | | digits | number |
Example
console.log(require('vv-shared').round(1.121212, 4))
split(string_for_split, left, right, [collapse_doubles]) ⇒ Array.<string>
split string, for example - '{asasdas}{234235}{}{vcvc}', and return array ['asasdas','234235','','vcvc']
Kind: global function
| Param | Type | Description | | --- | --- | --- | | string_for_split | any | | | left | string | left border | | right | string | right border | | [collapse_doubles] | 'no' | 'collapse_with_lower' | 'collapse_without_lower' | default = 'no' |
Example
console.log(require('vv-shared').split('{asasdas}{234235}{}{vcvc}','{','}'))
insertAt(string_where_insert, index, substring_for_replace) ⇒ string
insert substring in string
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_where_insert | any | string, where need insert | | index | number | position for insert substring | | substring_for_replace | any | substring |
Example
console.log(require('vv-shared').insertAt('ab',1,'XXX')) // return 'aXXXb'
console.log(require('vv-shared').insertAt(42,1,'Z')) // return '4Z2'
console.log(require('vv-shared').insertAt('ab',1,42)) // return 'a42b'
console.log(require('vv-shared').insertAt('ab',99,'X')) // return 'ab'
console.log(require('vv-shared').insertAt('ab','aa','X')) // return 'ab'
console.log(require('vv-shared').insertAt(undefined,1,'X')) // return empty string
console.log(require('vv-shared').insertAt('ab',1,undefined)) // return 'ab'
replaceAll(string_where_find, find, replace, [recursively]) ⇒ string
Replace all substring in string
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_where_find | any | string where find substring | | find | any | substring for find | | replace | any | substring for replace | | [recursively] | boolean | default = false |
Example
console.log(require('vv-shared').replaceAll('abcabc','b','X')) // return 'aXcaXc'
console.log(require('vv-shared').replaceAll('abcabc','B','X')) // return 'aXcaXc'
console.log(require('vv-shared').replaceAll(411,11,'2')) // return '42'
console.log(require('vv-shared').replaceAll('412',1,'')) // return '42'
console.log(require('vv-shared').replaceAll('412',undefined,undefined)) // return '412'
console.log(require('vv-shared').replaceAll('412','1',undefined)) // return '412'
console.log(require('vv-shared').replaceAll('412',undefined,'1')) // return '412'
format(string_for_format, [replaces]) ⇒ string
Replace substrings in string like format in c#
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_for_format | any | a string, on the basis of which to return a new formatted string | | [replaces] | any | Array.<any> | substitutions |
Example
console.log(require('vv-shared').format('Hello, {0}!','world')) // return 'Hello, world!'
console.log(require('vv-shared').format('Hello, {0} & {0}!','Johnson')) // return 'Hello, Johnson & Johnson!'
console.log(require('vv-shared').format('{0}, {1}!',['Hello','world'])) // return 'Hello, world!'
console.log(require('vv-shared').format('abc {0}','')) // return 'abc '
console.log(require('vv-shared').format('abc {0}',undefined)) // return 'abc '
console.log(require('vv-shared').format(42,'x')) // return '42'
console.log(require('vv-shared').format(undefined,'x')) // return empty string
formatExt(string_for_format, [replaces], [left], [right]) ⇒ string
Replace substrings in string like "format" with specify border characters
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_for_format | any | a string, on the basis of which to return a new formatted string | | [replaces] | any | Array.<any> | substitutions | | [left] | any | string left border | | [right] | any | string right border |
Example
console.log(require('vv-shared').formatExt('Hello, [[[0]]]!','world','[[[',']]]')) // return 'Hello, world!'
console.log(require('vv-shared').formatExt('Hello, {0} & {0}!','Johnson','{','}')) // return 'Hello, Johnson & Johnson!'
console.log(require('vv-shared').formatExt('{{0}}, {{1}}!',['Hello','world'], '{{', '}}')) // return 'Hello, world!'
formatDate(date, format) ⇒ string
Format date to string
Kind: global function
Returns: string - string or undefined
| Param | Type | Description | | --- | --- | --- | | date | any | date | | format | 23 | 111 | 112 | 114 | 1141 | 1122 | 1123 | 1124 | 126 | 10126 | 101262 | 101263 | 104 | 104108 | 1041082 | 1041083 | 1041084 | 'dy' | 'sd' | variants: 23 (yyyy-mm-dd), 111 (yyyy/mm/dd), 112 (yyyymmdd), 114 (hh:mi:ss:mmm), 1141 (hh:mi:ss.mmm), 1122 (yyyymmddhh), 1123 (yyyymmddhhmi), 1124 (yyyymmddhhmiss), 126 (yyyy-mm-ddThh:mi:ss.mmm), 10126 (yyyy-mm-dd-hh-mi-ss-mmm), 101262 (yyyy-mm-dd hh:mi:ss.mmm), 101263 (yyyy-mm-dd hh:mi), 104 (dd.mm.yyyy), 104108(dd.mm.yyyy hh:mi:ss), 1041082(dd.mm.yyyy hh:mi), 1041083(yyyy.mm.dd hh:mi), 1041084(yyyy.mm.dd hh:mi:ss), , 'dy' (string (length 3) with number day in year), 'sd' (string (length 3) with number second in day) |
Example
console.log(require('vv-shared').formatDate(new Date(),126)) // return current date as string in format yyyy-mm-ddThh:mi:ss.mmm
console.log(require('vv-shared').formatDate(new Date(),112)) // return current date as string in format yyyymmdd
cutFromArray(arr, index) ⇒ Array.<Object>
return array without one element - cut it (analog slice), but the original array does not change
Kind: global function
| Param | Type | | --- | --- | | arr | Array.<Object> | | index | number |
dateAdd(interval, value, date) ⇒ Date
increase (or decrease) date by second or minutes or hours or days
Kind: global function
| Param | Type | | --- | --- | | interval | 'second' | 'minute' | 'hour' | 'day' | | value | number | | date | any |
findPropertyInObject(object, property_name) ⇒ string
Search case insensitive property name in object
Kind: global function
Returns: string - property name - string or undefined
| Param | Type | Description | | --- | --- | --- | | object | any | object for search | | property_name | any | case insensitive property name |
Example
console.log(require('vv-shared').findPropertyInObject({a: 5},'a')) // return 'a'
console.log(require('vv-shared').findPropertyInObject({a: 5},'A')) // return 'a'
console.log(require('vv-shared').findPropertyInObject({a: 5},'b')) // return undefined
console.log(require('vv-shared').findPropertyInObject({a: 5},undefined)) // return undefined
console.log(require('vv-shared').findPropertyInObject(undefined,'a')) // return undefined
findPropertyExistsInObject(object, property_name) ⇒ boolean
Checking if exists insensitive property name in object
Kind: global function
Returns: boolean - always boolean (no undefined)
| Param | Type | Description | | --- | --- | --- | | object | any | object for search | | property_name | any | case insensitive property name |
Example
console.log(require('vv-shared').findPropertyExistsInObject({a: 5},'a')) // return true
console.log(require('vv-shared').findPropertyExistsInObject({a: 5},'A')) // return true
console.log(require('vv-shared').findPropertyExistsInObject({a: 5},'b')) // return false
console.log(require('vv-shared').findPropertyExistsInObject({a: 5},undefined)) // return false
console.log(require('vv-shared').findPropertyExistsInObject(undefined,'a')) // return false
findPropertyValueInObject(object, property_name, [default_value]) ⇒ any
Search value by case insensitive property name in object
Kind: global function
Returns: any - value or undefined
| Param | Type | Description | | --- | --- | --- | | object | any | object for search | | property_name | string | Array.<string> | case insensitive property name | | [default_value] | any | return this value, if property not find |
Example
console.log(require('vv-shared').findPropertyValueInObject({a: 5},'a')) // 5
console.log(require('vv-shared').findPropertyValueInObject({a: 5},'A')) // 5
console.log(require('vv-shared').findPropertyValueInObject({a: 5},'b')) // return undefined
console.log(require('vv-shared').findPropertyValueInObject({a: 5},undefined)) // return undefined
console.log(require('vv-shared').findPropertyValueInObject(undefined,'a')) // return undefined
findSubstrings(params) ⇒ Array.<type_findSubstrings_result>
Find substrings in text array
Kind: global function
Returns: Array.<type_findSubstrings_result> - array positions where find text
| Param | Type | | --- | --- | | params | type_findSubstrings |
findSubstrings~text_where_find_arr : Array.<string>
Kind: inner property of findSubstrings
border_add(string_where_add, [left], [right]) ⇒ string
For left and right in string add border string, if border not exists
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_where_add | any | string where need add border | | [left] | any | string left border for add | | [right] | any | string right border for add |
Example
console.log(require('vv-shared').border_add('aaa','[',']')) // return '[aaa]'
console.log(require('vv-shared').border_add('[aaa]','[',']')) // return '[aaa]'
console.log(require('vv-shared').border_add(42,'[',']')) // return '[42]'
console.log(require('vv-shared').border_add('b','*',undefined)) // return '*b'
console.log(require('vv-shared').border_add('*b','*',undefined)) // return '*b'
console.log(require('vv-shared').border_add(undefined,'[',']')) // return '[]'
border_del(string_where_find, [left], [right]) ⇒ string
For left and right in string remove border string, if border not exists
Kind: global function
Returns: string - always string (no undefined)
| Param | Type | Description | | --- | --- | --- | | string_where_find | any | string where need remove border | | [left] | any | string left border for remove | | [right] | any | string right border for remove |
Example
console.log(require('vv-shared').border_del('aaa','[',']')) // return 'aaa'
console.log(require('vv-shared').border_del('[aaa]','[',']')) // return 'aaa'
console.log(require('vv-shared').border_del(42,'[',']')) // return '42'
console.log(require('vv-shared').border_del('b','*',undefined)) // return 'b'
console.log(require('vv-shared').border_del('*b','*',undefined)) // return 'b'
console.log(require('vv-shared').border_del(undefined,'[',']')) // return empty string
text_page_char(text, page_size) ⇒ Array.<type_text_page_char>
Text pagination by char count per one page
Kind: global function
| Param | Type | | --- | --- | | text | string | | page_size | number |
text_page_byte(text, text_page_char_result) ⇒ Array.<type_text_page_byte>
Convert text_page_char result to byte markup for fs.createReadStream(...)
Kind: global function
| Param | Type | | --- | --- | | text | string | | text_page_char_result | Array.<type_text_page_char> |
guid()
Generate NON-UNIQUE guid by very-sery simple idea, based only on Math.random()
Kind: global function
parser(parser_options) ⇒ lib_parser
Simple parser for, example, js code or sql code
Kind: global function
| Param | Type | | --- | --- | | parser_options | parser_options |
Example
let parser = require('vv-shared').parser({ brackets: {left: '(', right: ')'}, end_of_command: [';'], string_border: ['"', "'"], one_string_comment: "//"})
let text = [
'let a = "hello!" // i am comment',
'let b = (2 + 3) * 5'
].join(require('os').EOL)
let a = parser.remove_comment(text)
let b = parser.lexemify_plain(text)
let c = parser.lexemify_tree(text)
readdir(dir, options, callback)
Recursive scan directory
Kind: global function
| Param | Type | | --- | --- | | dir | string | | options | type_readdir_options | | callback | callback_readdir |
Example
require('vv-shared').readdir(__dirname, undefined, (error, files) => {console.log(files)} )
type_findSubstrings
Kind: global typedef
Properties
| Name | Type | Description | | --- | --- | --- | | text_where_find | string | Array.<string> | text where need find | | text_find | string | text find | | [divider_find] | string | how split text_find in substring | | [matchCase] | boolean | default false | | [position] | boolean | allow position in text find lexems, default false |
type_findSubstrings_result
Kind: global typedef
Properties
| Name | Type | | --- | --- | | start | number | | end | number | | line | number |
type_text_page_char
Kind: global typedef
Properties
| Name | Type | | --- | --- | | step | number | | position_start | number | | text_length | number | | offset_length | number |
type_text_page_byte
Kind: global typedef
Properties
| Name | Type | | --- | --- | | step | number | | position_start | number | | position_end | number |
parser_options : lib_parser.type_options
Kind: global typedef
parser_lexem : lib_parser.type_lexem
Kind: global typedef
parser_lexem_type : lib_parser.type_lexem_type
Kind: global typedef
type_readdir_options
Kind: global typedef
Properties
| Name | Type | | --- | --- | | mode | 'files' | 'paths' | 'all' |
type_readdir
Kind: global typedef
Properties
| Name | Type | | --- | --- | | path | string | | file | string | | fsstat | fs.Stats |