libT
Javascript library for object manipulation
npm install --save libt
copy
var T = require('libt')
var obj1 = {
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
}
var obj2 = T.copy(obj1)
obj2.info.sons[0].age = 4
log(obj2.info.sons[0].age)
log(obj1.info.sons[0].age)
compose
var T = require('libt')
var divide2 = x => x / 2
var multiply3 = x => x * 3
var add5 = x => x + 5
log(T.compose(divide2, multiply3, add5)(7))
log(T.compose(multiply3, add5, divide2)(7))
log(T.compose(add5, divide2, multiply3)(7))
log(T.compose()(7))
path
var T = require('libt')
log(T.path({
x: 'name',
y: 'age',
z: {
x: 'fruits.2',
y: 'info.sons.0.age'
}
})({
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
}))
{
"x": "John",
"y": 35,
"z": {
"x": "manga",
"y": 3
}
}
iterate
var T = require('libt')
var R = []
T.iterate(x => {
R.push(x)
})({
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
})
log(R)
[
"John",
35,
"apple",
"banana",
"manga",
"Clara",
3,
"Pedro",
2,
null
]
var T = require('libt')
var P = []
T.iterate((x, p) => {
P.push(p)
})({
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
})
log(P)
[
"name",
"age",
"fruits.0",
"fruits.1",
"fruits.2",
"info.sons.0.name",
"info.sons.0.age",
"info.sons.1.name",
"info.sons.1.age",
"job"
]
set
var T = require('libt')
var obj = {
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
}
log(T.set('job.company.name', 'yyz')(obj))
{
"name": "John",
"age": 35,
"fruits": ["apple", "banana", "manga"],
"info": {
"sons": [
{
"name": "Clara",
"age": 3
}, {
"name": "Pedro",
"age": 2
}
]
},
"job": {
"company": {
"name": "yyz"
}
}
}
log(T.set('info.sons.0')(obj))
{
"name": "John",
"age": 35,
"fruits": ["apple", "banana", "manga"],
"info": {
"sons": [
{
"name": "Pedro",
"age": 2
}
]
},
"job": null
}
get
var T = require('libt')
var obj = {
name: 'John',
age: 35,
fruits: ['apple', 'banana', 'manga'],
info: {
sons: [
{
name: 'Clara',
age: 3
}, {
name: 'Pedro',
age: 2
}
]
},
job: null
}
log(T.get('fruits.1')(obj))
log(T.get('age')(obj))
log(T.get('job')(obj))
log(T.get('info.sons.1.name')(obj))
log(T.get('info.sons.5.age')(obj))
compare
var T = require('libt')
var obj1 = {
x: 'banana',
y: 5,
z: {
a: 7,
b: [1, 4, 5, 7],
z: 12
}
}
var obj2 = T.copy(obj1)
log(T.compare(obj1)(obj2))
obj2.z.b[0] = 0
log(T.compare(obj1)(obj2))
obj2.z.b[0] = 2
log(T.compare(obj1)(obj2))
sort
var T = require('libt')
var V = [2, 8, 1, 7, 19, 32, 4, 6]
log(T.sort()(V))
log(T.sort('-')(V))
log(T.sort(['tag', '-id'])([
{id: 2, tag: 'plane'},
{id: 8, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 7, tag: 'car'},
{id: 2, tag: 'train'},
{id: 8, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'bike'}
]))
[
{"id": 7, "tag": "bike"},
{"id": 1, "tag": "bike"},
{"id": 7, "tag": "car"},
{"id": 1, "tag": "car"},
{"id": 8, "tag": "plane"},
{"id": 2, "tag": "plane"},
{"id": 8, "tag": "train"},
{"id": 2, "tag": "train"}
]
distinct
var T = require('libt')
log(T.distinct()([1, 5, 6, 8, 4, 2, 4, 1, 4, 2, 3, 7, 8]))
log(T.distinct({tag: 'tag'})([
{id: 2, tag: 'plane'},
{id: 8, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 7, tag: 'car'},
{id: 2, tag: 'train'},
{id: 8, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'bike'}
]))
[
{"tag": "bike"},
{"tag": "car"},
{"tag": "plane"},
{"tag": "train"}
]
where
var T = require('libt')
log(T.where([
{
path: 'id',
operator: '>',
value: 2
}, {
path: 'tag',
operator: '===',
value: 'car'
}
])([
{id: 2, tag: 'plane'},
{id: 8, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 7, tag: 'car'},
{id: 2, tag: 'train'},
{id: 8, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'bike'}
]))
[
{"id": 7, "tag": "car"}
]
var T = require('libt')
log(T.where([
{
path: 'tag',
operator: '~',
value: 'ar'
}
])([
{id: 2, tag: 'plane'},
{id: 8, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 7, tag: 'car'},
{id: 2, tag: 'train'},
{id: 8, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'bike'}
]))
[
{"id": 1, "tag": "car"},
{"id": 7, "tag": "car"}
]
merge
var T = require('libt')
log(T.merge({test: 'merge', fruit: 'apple'})({help: 'please', test: 'merge2'}))
{
"test": "merge2",
"fruit": "apple",
"help": "please"
}
var T = require('libt')
log(T.merge([
{test: 'merge', fruit: 'apple'},
{test: 'array', fruit: 'banana'},
{test: 'first', fruit: 'orange'}
])({help: 'please', test: 'merge2'}))
[
{"test": "merge2", "fruit": "apple", "help": "please"},
{"test": "merge2", "fruit": "banana", "help": "please"},
{"test": "merge2", "fruit": "orange", "help": "please"}
]
group
var T = require('libt')
log(T.group({}, {N: 'sum("1")', Total: 'sum("$.id")'})([
{id: 2, tag: 'plane'},
{id: 8, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 7, tag: 'car'},
{id: 2, tag: 'train'},
{id: 8, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'bike'}
]))
[
{"N": 8, "Total": 36}
]
var T = require('libt')
log(T.group({tag: 'tag'}, {N: 'sum("1")', Total: 'sum("$.id")'})([
{id: 2, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 8, tag: 'train'},
{id: 8, tag: 'plane'},
{id: 2, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'car'},
{id: 7, tag: 'bike'}
]))
[
{"tag": "bike", "N": 2, "Total": 8},
{"tag": "car", "N": 2, "Total": 8},
{"tag": "plane", "N": 2, "Total": 10},
{"tag": "train", "N": 2, "Total": 10}
]
pager
var T = require('libt')
var V = [
{id: 2, tag: 'plane'},
{id: 1, tag: 'car'},
{id: 8, tag: 'train'},
{id: 8, tag: 'plane'},
{id: 2, tag: 'train'},
{id: 1, tag: 'bike'},
{id: 7, tag: 'car'},
{id: 7, tag: 'bike'}
]
log(T.pager(3)(1)(V))
[
{"id": 2, "tag": "plane"},
{"id": 1, "tag": "car"},
{"id": 8, "tag": "train"}
]
[
{"id": 8, "tag": "plane"},
{"id": 2, "tag": "train"},
{"id": 1, "tag": "bike"}
]
[
{"id": 7, "tag": "car"},
{"id": 7, "tag": "bike"}
]
evaluate
var T = require('libt')
log(T.evaluate('1')())
log(T.evaluate('true')())
log(T.evaluate('3 * 7')())
log(T.evaluate('3 * $')(12))
log(T.evaluate('"Me"')())
parse
var T = require('libt')
log(T.parse('integer')('-21.c'), -21)
log(T.parse('integer')('-21.3c'), -21)
log(T.parse('number')('-21.2c'), -21.2)
log(T.parse('number:2')('-21,386c'), -21.39)
log(T.parse('number:3')(3.4567), 3.457)
log(T.parse('date')('2018-01-01zzzzzz'), '2018-01-01')
log(T.parse('date')('2018-01'), '')
log(T.parse('string:6')('Albert Eistein'), 'Albert')
log(T.parse('string:lower')('Albert Eistein'), 'albert eistein')
log(T.parse('string:upper')('Albert Eistein'), 'ALBERT EISTEIN')
log(T.parse('string:6:lower')('Albert Eistein'), 'albert')
log(T.parse('string:6:upper')('Albert Eistein'), 'ALBERT')
match
var T = require('libt')
log(T.match('integer')(143.56))
log(T.match('integer')(143))
log(T.match('number')('-91.4'))
log(T.match('number')(143.56))
log(T.match('number')('true'))
log(T.match('date')('1879-03-14'))
log(T.match('date')('test'))
log(T.match('text')('one line\ntwo lines'))
log(T.match('text')('only one line'))
log(T.match('bool')('100011010101'))
log(T.match('bool')('1001011101x'))
log(T.match('hex')('ab4371FE'))
log(T.match('hex')('34t56Ad'))
log(T.match(/abc/)('abc dfg'))
log(T.match(/abc$/)('abc dfg'))
randomId
var T = require('libt')
log(T.match(/^[\d]{20}$/)(T.randomId('int')(20)))
log(T.match(/^[a-z]{20}$/)(T.randomId('char')(20)))
log(T.match(/^[a-f\d]{20}$/)(T.randomId('hex')(20)))
log(T.match(/^[0-1]{20}$/)(T.randomId('bool')(20)))
log(T.match(/^[a-z\d]{20}$/)(T.randomId()(20)))
replaceAll
var T = require('libt')
log(T.replaceAll(' ')('Hello! My name is Mario!'))
log(T.replaceAll(' ', '*')('Hello! My name is Mario!'))
log(T.replaceAll(', ', ';')('A, B, C, D, F, G'))