5.0.4 • Published 3 years ago

vv-shared v5.0.4

Weekly downloads
98
License
MIT
Repository
github
Last release
3 years ago

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

ParamTypeDescription
objectanyobject for check

isEmptyString(object) ⇒ boolean

isEmpty + .trim() + check len > 0

Kind: global function

ParamTypeDescription
objectanyobject for check

isFunction(object) ⇒ boolean

Check object for function

Kind: global function

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('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

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('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

ParamTypeDescription
valuenumberint for add phrase
phrase_onestring
phrase_twostring
phrase_fewstring

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

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

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

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

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

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

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

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

Kind: global function

ParamType
valueBuffer | Array.<Number>
default_valueBuffer

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('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

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

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

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('vv-shared').toErrorMessage(error, 'in myFunction({0})','value')}

toHtml(value) ⇒ string

Return string for inject in html

Kind: global function

ParamType
valuestring

roundFload(value, digits) ⇒ number

True round

Kind: global function

ParamType
valuenumber
digitsnumber

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

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

ParamTypeDescription
string_where_insertanystring, where need insert
indexnumberposition for insert substring
substring_for_replaceanysubstring

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)

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

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

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('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

ParamTypeDescription
dateanydate
format23 | 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

ParamType
arrArray.<Object>
indexnumber

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('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)

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

ParamTypeDescription
objectanyobject for search
property_namestring | Array.<string>case insensitive property name
default_valueanyreturn 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

ParamType
paramstype_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)

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

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

ParamType
textstring
page_sizenumber

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

ParamType
textstring
text_page_char_resultArray.<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

ParamType
parser_optionsparser_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

ParamType
dirstring
optionstype_readdir_options
callbackcallback_readdir

Example

require('vv-shared').readdir(__dirname, undefined, (error, files) => {console.log(files)} )

type_findSubstrings

Kind: global typedef
Properties

NameTypeDescription
text_where_findstring | Array.<string>text where need find
text_findstringtext find
divider_findstringhow split text_find in substring
matchCasebooleandefault false
positionbooleanallow position in text find lexems, default false

type_findSubstrings_result

Kind: global typedef
Properties

NameType
startnumber
endnumber
linenumber

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

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

NameType
mode'files' | 'paths' | 'all'

type_readdir

Kind: global typedef
Properties

NameType
pathstring
filestring
fsstatfs.Stats
5.0.4

3 years ago

5.0.2

3 years ago

4.0.11

3 years ago

4.0.10

3 years ago

4.0.7

3 years ago

4.0.6

3 years ago

4.0.9

3 years ago

4.0.8

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.1

3 years ago

4.0.2

3 years ago

3.0.25

3 years ago

3.0.24

3 years ago

3.0.23

3 years ago

3.0.21

3 years ago

3.0.22

3 years ago

3.0.17

3 years ago

3.0.16

3 years ago

3.0.15

3 years ago

3.0.14

4 years ago

3.0.13

4 years ago

3.0.12

4 years ago

3.0.9

4 years ago

3.0.10

4 years ago

3.0.11

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

2.0.46

4 years ago

2.0.45

4 years ago

2.0.44

4 years ago

2.0.43

4 years ago

2.0.42

4 years ago

2.0.40

4 years ago

2.0.41

4 years ago

2.0.39

4 years ago

2.0.38

4 years ago

2.0.37

4 years ago

2.0.35

4 years ago

2.0.33

4 years ago

2.0.31

4 years ago

2.0.32

4 years ago

2.0.30

4 years ago

2.0.29

4 years ago

2.0.28

4 years ago

2.0.27

4 years ago

2.0.25

4 years ago

2.0.24

4 years ago

2.0.23

4 years ago

2.0.22

4 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.17

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago