1.0.61 • Published 5 years ago

viva-convert v1.0.61

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
5 years ago

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)

ParamTypeDescription
objectanyobject 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)

ParamTypeDescription
objectanyobject for check

isFunction(object) ⇒ boolean

Check object for function

Kind: global function
Returns: boolean - always boolean (no undefined)

ParamTypeDescription
objectanyobject for check

isGuid(object) ⇒ boolean

Check object for GUID

Kind: global function
Returns: boolean - always boolean (no undefined)

ParamTypeDescription
objectanyobject 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

ParamType
object1any
object2any
object3any
object4any
object5any

equal(object1, object2) ⇒ boolean

Equal two objects

Kind: global function

ParamType
object1any
object2any

duplicates(where_find_duplicates) ⇒ Array.<string>

Return array with duplicates items from string array (after trim and toLowerCase)

Kind: global function

ParamType
where_find_duplicatesArray.<string>

toString(value, default_value) ⇒ string

Convert object to string

Kind: global function
Returns: string - string or undefined

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valuenumberint for add phrase
phrase_onestring
phrase_twostring
phrase_fewstring

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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject for convert
default_valueanydefault 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

ParamTypeDescription
valueanyobject 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)

ParamTypeDescription
charanychar or string for fill
countnumbercount 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)

ParamTypeDescription
errorany
prefixstringprefix text for error message
replacesanysubstitutions 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

ParamTypeDescription
string_for_splitany
leftstringleft border
leftstringright 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)

ParamTypeDescription
string_where_insertanystring, where need insert
indexnumberposition for insert substring
substring_for_replaceanysubstring

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)

ParamTypeDescription
string_where_findanystring where find substring
findanysubstring for find
replaceanysubstring for replace
recursivelybooleandefault 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)

ParamTypeDescription
string_for_formatanya string, on the basis of which to return a new formatted string
replacesany | 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)

ParamTypeDescription
string_for_formatanya string, on the basis of which to return a new formatted string
replacesany | Array.<any>substitutions
leftanystring left border
rightanystring 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

ParamTypeDescription
dateanydate
formatanyvariants: 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

ParamType
dateany

dateAdd(interval, value, date) ⇒ Date

increase (or decrease) date by second or minutes or hours or days

Kind: global function

ParamType
interval'second' | 'minute' | 'hour' | 'day'
valuenumber
dateany

findPropertyInObject(object, property_name) ⇒ string

Search case insensitive property name in object

Kind: global function
Returns: string - property name - string or undefined

ParamTypeDescription
objectanyobject for search
property_nameanycase 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)

ParamTypeDescription
objectanyobject for search
property_nameanycase 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

ParamTypeDescription
objectanyobject for search
property_nameanycase insensitive property name
default_valueanyreturn 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)

ParamTypeDescription
string_where_addanystring where need add border
leftanystring left border for add
rightanystring 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)

ParamTypeDescription
string_where_findanystring where need remove border
leftanystring left border for remove
rightanystring 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

ParamType
textstring
page_sizenumber

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

ParamType
textstring
text_page_char_resultArray.<type_text_page_char>

type_text_page_char

Kind: global typedef
Properties

NameType
stepnumber
position_startnumber
text_lengthnumber
offset_lengthnumber

type_text_page_byte

Kind: global typedef
Properties

NameType
stepnumber
position_startnumber
position_endnumber
1.0.61

5 years ago

1.0.60

5 years ago

1.0.59

5 years ago

1.0.58

5 years ago

1.0.57

5 years ago

1.0.56

5 years ago

1.0.55

6 years ago

1.0.54

6 years ago

1.0.53

6 years ago

1.0.52

6 years ago

1.0.51

6 years ago

1.0.50

6 years ago

1.0.49

6 years ago

1.0.48

6 years ago

1.0.47

6 years ago

1.0.45

6 years ago

1.0.44

6 years ago

1.0.43

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

7 years ago

1.0.30

7 years ago

1.0.29

7 years ago

1.0.28

7 years ago

1.0.27

7 years ago

1.0.25

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.10

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago