viva-convert
v1.0.61
Published
functions to convert values from different types
Downloads
25
Readme
Members
Functions
Typedefs
viva-convert: (license MIT) shared functions for work with objects
Kind: global variable
Author: Vitalii vasilev
License: MIT
isAbsent(object) ⇒ boolean
Check object for undefined, wrapper for "typeof object === 'undefined'"
Kind: global function
Returns: boolean - always boolean (no undefined)
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
isEmpty(object) ⇒ boolean
Check object for empty. An empty object is undefined, null, an empty string, NaN
Kind: global function
Returns: boolean - always boolean (no undefined)
| Param | Type | Description | | --- | --- | --- | | object | any | object for check |
isFunction(object) ⇒ boolean
Check object for function
Kind: global function
Returns: boolean - always boolean (no undefined)
| 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('viva-convert').isGuid(undefined)) // return false
Example
console.log(require('viva-convert').isGuid('')) // return false
Example
console.log(require('viva-convert').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('viva-convert').toString(undefined)) // return undefined
Example
console.log(require('viva-convert').toString(undefined,'my default string')) // return 'my default string'
Example
console.log(require('viva-convert').toString({a: 5},'my default string')) // return 'my default string'
Example
console.log(require('viva-convert').toString([1,2,3],'my default string')) // return 'my default string'
Example
console.log(require('viva-convert').toString('','my default string')) // return empty string
Example
console.log(require('viva-convert').toString(new Date(),'my default string')) // return formatDate(..., 126)
Example
console.log(require('viva-convert').toString(45,'my default string')) // return '45'
Example
console.log(require('viva-convert').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('viva-convert').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('viva-convert').toInt('abc')) // return undefined
Example
console.log(require('viva-convert').toInt('abc','xyz')) // return undefined
Example
console.log(require('viva-convert').toInt('77',42)) // return 77
Example
console.log(require('viva-convert').toInt('-77',42)) // return -77
Example
console.log(require('viva-convert').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('viva-convert').toFloat('abc')) // return undefined
Example
console.log(require('viva-convert').toFloat('abc','xyz')) // return undefined
Example
console.log(require('viva-convert').toFloat('abc','-42.42')) // return -42.42
Example
console.log(require('viva-convert').toFloat('77',42)) // return 77
Example
console.log(require('viva-convert').toFloat('77.2',42)) // return 77.2
Example
console.log(require('viva-convert').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('viva-convert').toBool(2)) // return undefined
Example
console.log(require('viva-convert').toBool('abc')) // return undefined
Example
console.log(require('viva-convert').toBool('abc','false')) // return false
Example
console.log(require('viva-convert').toBool(0)) // return true
Example
console.log(require('viva-convert').toBool(1)) // return true
Example
console.log(require('viva-convert').toBool('TruE')) // return true
Example
console.log(require('viva-convert').toBool('true')) // return true
Example
console.log(require('viva-convert').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('viva-convert').toGuid(undefined)) // return undefined
Example
console.log(require('viva-convert').toGuid('')) // return undefined
Example
console.log(require('viva-convert').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('viva-convert').toDate('2018-04-12T16:35:49')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.1')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.12')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.123')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.1234')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.12345')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.123456')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.1234567')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.12345678')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.123456789')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.123+03:00')) // return true date WITH CURRENT GMT (ignore +03 from example)
Example
console.log(require('viva-convert').toDate('2018-04-12T16:35:49.123Z')) // return true date
Example
console.log(require('viva-convert').toDate('2018-04-12')) // return true date
Example
console.log(require('viva-convert').toDate('12.04.2018')) // return true date
Example
console.log(require('viva-convert').toDate('12.04.2018 16:35')) // return true date
Example
console.log(require('viva-convert').toDate('12.04.2018 16:35:49')) // return true date
Example
console.log(require('viva-convert').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 function 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 function based on toDate() with change day,month,year to 01.01.1900, get them examples and change toDate to toTime
Example
console.log(require('viva-convert').toTime('16:35')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.1')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.12')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.123')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.1234')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.12345')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.123456')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.1234567')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.12345678')) // return true date
Example
console.log(require('viva-convert').toTime('16:35.49.123456789')) // return true date
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('viva-convert').toIp('LOCALHOST')) // return 'localhost'
Example
console.log(require('viva-convert').toIp('localhost')) // return 'localhost'
Example
console.log(require('viva-convert').toIp('192.168.1.2')) // return '192.168.1.2'
Example
console.log(require('viva-convert').toIp('abc','192.168.1.2')) // return '192.168.1.2'
Example
console.log(require('viva-convert').toIp('abc')) // return undefined
toArray(value, [type]) ⇒ Array.<Object>
Convert object to array
Kind: global function
| Param | Type | Description | | --- | --- | --- | | value | 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 | any | char or string for fill | | count | number | count fill |
Example
console.log(require('viva-convert').toCharArray('abc',2)) // return 'abcabc'
Example
console.log(require('viva-convert').toCharArray('abc','2')) // return 'abcabc'
Example
console.log(require('viva-convert').toCharArray(42,2)) // return 4242
Example
console.log(require('viva-convert').toCharArray(undefined,2)) // return empty string
Example
console.log(require('viva-convert').toCharArray('abc',undefined)) // return empty string
toErrorMessage(error, [prefix], [replaces], [sourсe]) ⇒ string
Returns a nice formatted error message - wrapper for function 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('viva-convert').toErrorMessage(error, 'in myFunction({0})','value')}
split(string_for_split, left, left, [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 | | left | string | right border | | [collapse_doubles] | 'no' | 'collapse_with_lower' | 'collapse_without_lower' | default 'no' |
Example
console.log(require('viva-convert').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('viva-convert').insertAt('ab',1,'XXX')) // return 'aXXXb'
Example
console.log(require('viva-convert').insertAt(42,1,'Z')) // return '4Z2'
Example
console.log(require('viva-convert').insertAt('ab',1,42)) // return 'a42b'
Example
console.log(require('viva-convert').insertAt('ab',99,'X')) // return 'ab'
Example
console.log(require('viva-convert').insertAt('ab','aa','X')) // return 'ab'
Example
console.log(require('viva-convert').insertAt(undefined,1,'X')) // return empty string
Example
console.log(require('viva-convert').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('viva-convert').replaceAll('abcabc','b','X')) // return 'aXcaXc'
Example
console.log(require('viva-convert').replaceAll('abcabc','B','X')) // return 'aXcaXc'
Example
console.log(require('viva-convert').replaceAll(411,11,'2')) // return '42'
Example
console.log(require('viva-convert').replaceAll('412',1,'')) // return '42'
Example
console.log(require('viva-convert').replaceAll('412',undefined,undefined)) // return '412'
Example
console.log(require('viva-convert').replaceAll('412','1',undefined)) // return '412'
Example
console.log(require('viva-convert').replaceAll('412',undefined,'1')) // return '412'
format(string_for_format, [replaces]) ⇒ string
Replace substrings in string like format function 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('viva-convert').format('Hello, {0}!','world')) // return 'Hello, world!'
Example
console.log(require('viva-convert').format('Hello, {0} & {0}!','Johnson')) // return 'Hello, Johnson & Johnson!'
Example
console.log(require('viva-convert').format('{0}, {1}!',['Hello','world'])) // return 'Hello, world!'
Example
console.log(require('viva-convert').format('abc {0}','')) // return 'abc '
Example
console.log(require('viva-convert').format('abc {0}',undefined)) // return 'abc '
Example
console.log(require('viva-convert').format(42,'x')) // return '42'
Example
console.log(require('viva-convert').format(undefined,'x')) // return empty string
formatExt(string_for_format, [replaces], [left], [right]) ⇒ string
Replace substrings in string like function "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('viva-convert').formatExt('Hello, [[[0]]]!','world','[[[',']]]')) // return 'Hello, world!'
Example
console.log(require('viva-convert').formatExt('Hello, {0} & {0}!','Johnson','{','}')) // return 'Hello, Johnson & Johnson!'
Example
console.log(require('viva-convert').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 | any | variants: 112 (yyyymmdd), 126 (yyyy-mm-ddThh:mi:ss.mmm), 10126 (yyyy-mm-dd-hh-mi-ss-mmm), 104 (dd.mm.yyyy), 104108(dd.mm.yyyy hh:mm:ss), 1041082(dd.mm.yyyy hh:mm), 1041083(yyyy.mm.dd hh:mm) |
Example
console.log(require('viva-convert').formatDate(new Date(),126)) // return current date as string in format yyyy-mm-ddThh:mi:ss.mmm
Example
console.log(require('viva-convert').formatDate(new Date(),112)) // return current date as string in format yyyymmdd
formatDayOfYear([date])
Kind: global function
| Param | Type | | --- | --- | | [date] | any |
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('viva-convert').findPropertyInObject({a: 5},'a')) // return 'a'
Example
console.log(require('viva-convert').findPropertyInObject({a: 5},'A')) // return 'a'
Example
console.log(require('viva-convert').findPropertyInObject({a: 5},'b')) // return undefined
Example
console.log(require('viva-convert').findPropertyInObject({a: 5},undefined)) // return undefined
Example
console.log(require('viva-convert').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('viva-convert').findPropertyExistsInObject({a: 5},'a')) // return true
Example
console.log(require('viva-convert').findPropertyExistsInObject({a: 5},'A')) // return true
Example
console.log(require('viva-convert').findPropertyExistsInObject({a: 5},'b')) // return false
Example
console.log(require('viva-convert').findPropertyExistsInObject({a: 5},undefined)) // return false
Example
console.log(require('viva-convert').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 | any | case insensitive property name | | [default_value] | any | return this value, if property not find |
Example
console.log(require('viva-convert').findPropertyValueInObject({a: 5},'a')) // 5
Example
console.log(require('viva-convert').findPropertyValueInObject({a: 5},'A')) // 5
Example
console.log(require('viva-convert').findPropertyValueInObject({a: 5},'b')) // return undefined
Example
console.log(require('viva-convert').findPropertyValueInObject({a: 5},undefined)) // return undefined
Example
console.log(require('viva-convert').findPropertyValueInObject(undefined,'a')) // return undefined
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('viva-convert').border_add('aaa','[',']')) // return '[aaa]'
Example
console.log(require('viva-convert').border_add('[aaa]','[',']')) // return '[aaa]'
Example
console.log(require('viva-convert').border_add(42,'[',']')) // return '[42]'
Example
console.log(require('viva-convert').border_add('b','*',undefined)) // return '*b'
Example
console.log(require('viva-convert').border_add('*b','*',undefined)) // return '*b'
Example
console.log(require('viva-convert').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('viva-convert').border_del('aaa','[',']')) // return 'aaa'
Example
console.log(require('viva-convert').border_del('[aaa]','[',']')) // return 'aaa'
Example
console.log(require('viva-convert').border_del(42,'[',']')) // return '42'
Example
console.log(require('viva-convert').border_del('b','*',undefined)) // return 'b'
Example
console.log(require('viva-convert').border_del('*b','*',undefined)) // return 'b'
Example
console.log(require('viva-convert').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 function 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> |
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 |