@nexssp/extend
15.01.2022 Upgrade - Now works also with import
as module.
const array = require('@nexssp/extend/array')
console.log(Object.keys(array))
import { remove, flat, range } from '@nexssp/extend/array'
Some functions written/re-written and/or collected to make things easier in JavaScript/NodeJS.
How to Start?
const extend = require('@nexssp/extend/array')
New range function
range(0, 63)
filterArray
filterArray([1, 2, 3, 4], [2, 3])
New Object dot notation (no eval!)
dset({}, 'a.b.c.d.a.b.a', { x: 1, y: 2 })
const x = {
1: 2,
[',:$']: { w: { eeee: 5 } },
x: 12222,
y: { z: 1345 },
}
console.log(dget(x, ',:$.w.eeee'))
New String.addTimestamp and String.trimExtension
addTimestamp('mystring')
addTimestamp('myfile.png')
trimExtension('myfile.png')
New Object.findByProp() and Object.deleteByProp()
Object.findByProp()
Now also with regexp
findByProp(
{
commands: [
{ name: 'a1', 'something else': 'x' },
{ name: 'a2', 'something else': 'y' },
],
key2: 'val2',
},
'commands',
'name',
'a2'
)
Object.deleteByProp()
deleteByProp(
{
commands: [
{ name: 'a1', 'something else': 'x' },
{ name: 'a2', 'something else': 'y' },
],
key2: 'val2',
},
'commands',
'name',
'a1'
)
New
argvAddQuotes([ 'nexss','par1','par with space' ,'--x=a b c' ,"y='a c d'",'a b c' ]
) ==>
[ 'nexss','par1','"par with space"','--x="a b c"','y="a c d"','"a b c"']
New YAML and JSON with 'function' serialize
Yaml
const { YAMLstringify, YAMLparse } = extend('@nexssp/extend/yaml')
const yamlFromObject = YAMLstringify({
x: 1,
y: 5,
nested: { x: 5, nested: { t: 'test' } },
})
console.log('YAMLfromObject:', yamlFromObject)
console.log('ObjectFromYaml', YAMLparse(yamlFromObject))
JSON
const { JSONstringify, JSONparse } = extend('@nexssp/extend/json')
const x = JSONstringify({
x: 1,
y: function (e) {
console.log(`Hello! ${e}}`)
},
})
console.log(JSONparse(x))
New Object functions
const { invert } = extend('@nexssp/extend/object')
invert({ key1: 'val1', key2: 'val2' })
NEW for Strings and Arrays
- argStripQuotes() - "test1" => test2 OR more advanced 'someadvanced="sd asd asd asd=,$$$=!"' => someadvanced=sd asd asd asd=,$$$=!
NEW for Strings
- StripTerminalColors() - removes terminal colors
- StripEndQuotes() - "test1" => test2
Usage
Strings
argStripQuotes("mystring='xxx'")
stripEndQuotes('"test2"')
pad('Some string', 20, '=')
similarity('abc def', 'abc deg')
camelCase('this is a test')
template('this ${myvar} and it is ${myvar2}', {
myvar: 'works!',
myvar2: 'amazing!',
})
Arrays
argStripQuotes([
"myparam1='test1'",
'myparam2="test2"',
'someadvanced="sd asd asd asd=,$$$=!"',
'"Nexss Programmer"="test more adv"',
]);
remove(["my val1", "my second val",
"and another val"],"my second val");
flat([[1, 2], [3, 4]]);
flat([1, [2, [5, 2], [6]]]);
flat([1, [2, [5, 2, [6]]],2);