nv-facutil-multi-key
v1.0.2
Published
nv-facutil-multi-key ======================== - nv-facutil-multi-key is a simple util for multi-key dict
Downloads
7
Maintainers
Readme
nv-facutil-multi-key
- nv-facutil-multi-key is a simple util for multi-key dict
install
- npm install nv-facutil-multi-key
usage
const {MultiKey,empty,ERROR_DICT} = require("nv-facutil-multi-key");
example
for test
var area = {
succ$0: 'success',
province$1: 'province-not-valid',
city$2: 'city-not-valid',
county$3: 'county-not-valid',
length$4: 'length-must-be-6'
}
var birth = {
succ$0:"success",
length$1:"length-must-be-8",
year$2:"must-be-human",
month$3:"month-must-between-01-12",
day$4:"day-must-between-01-28,29,30,31"
}
var sex = {
succ$0:"success",
length$1:"length-must-be-1",
value$2:"value-must-be-1,3,5,7,9(man)-or-0,2,4,6,8(woman)"
}
var police = {
succ$0:"success",
length$1:"length-must-be-2",
value$2:"value-must-be-^[0-9]{2}$"
}
var parity = {
succ$0:"success",
length$1:"length-must-be-1",
value$2:"value-must-be-^[0-9X]$",
parity$3:"parity-error"
}
var parent_err ={
succ$0:"success",
area$1:area,
birth$2:birth,
sex$3:sex,
police$4:police,
parity$5:parity
}
MultiKey
//
var serr = new MultiKey(parent_err,'$')
/*
> serr
{
'succ$0': 'success',
'area$1': {
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
},
'birth$2': {
'succ$0': 'success',
'length$1': 'length-must-be-8',
'year$2': 'must-be-human',
'month$3': 'month-must-between-01-12',
'day$4': 'day-must-between-01-28,29,30,31'
},
'sex$3': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-1,3,5,7,9(man)-or-0,2,4,6,8(woman)'
},
'police$4': {
'succ$0': 'success',
'length$1': 'length-must-be-2',
'value$2': 'value-must-be-^[0-9]{2}$'
},
'parity$5': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
}
> serr.$is_root()
true
>
*/
get
//multi-key
serr.area
serr[1]
serr[1] === serr.area
/*
> serr.area
{
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
}
> serr[1]
{
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
}
> serr[1] === serr.area
true
>
*/
serr.area.city
serr.area[2]
serr[1][2]
serr[1].city
serr.area.city === serr.area[2]
serr.area.city === serr[1][2]
serr.area.city === serr[1].city
/*
> serr.area.city
city-not-valid
> serr.area[2]
city-not-valid
> serr[1][2]
city-not-valid
> serr[1].city
city-not-valid
>
> serr.area.city === serr.area[2]
true
> serr.area.city === serr[1][2]
true
> serr.area.city === serr[1].city
true
>
*/
//use .$json() to get the real-value
serr.area.city.$json()
/*
> serr.area.city.$json()
'city-not-valid'
>
*/
relation
//relation
serr.area.city
serr.area.city.$parent
serr.area.$children
serr.area.city.$is_child_of(serr.area)
serr.area.city.$is_des_of(serr)
serr.area.city.$pl
serr.area.city.$ances
/*
> serr.area.city
city-not-valid
> serr.area.city.$parent
{
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
}
> serr.area.$children
[
success,
province-not-valid,
city-not-valid,
county-not-valid,
length-must-be-6
]
> serr.area.city.$is_child_of(serr.area)
true
> serr.area.city.$is_des_of(serr)
true
> serr.area.city.$pl
[ 'area$1', 'city$2' ]
> serr.area.city.$ances.map(nd=>nd.$pkey)
[ Symbol(empty), 'area$1' ]
>
serr.area.city.$ances[0].$is_root()
>true
*/
serr.$deses.filter(nd=>nd.$is_leaf())
/*
[
success,
success,
province-not-valid,
city-not-valid,
county-not-valid,
length-must-be-6,
success,
length-must-be-8,
must-be-human,
month-must-between-01-12,
day-must-between-01-28,29,30,31,
success,
length-must-be-1,
value-must-be-1,3,5,7,9(man)-or-0,2,4,6,8(woman),
success,
length-must-be-2,
value-must-be-^[0-9]{2}$,
success,
length-must-be-1,
value-must-be-^[0-9X]$,
parity-error
]
*/
pathlist get set
serr.$plget([1,2])
serr.$plget(['area','city'])
/*
> serr.$plget([1,2])
city-not-valid
> serr.$plget(['area','city'])
city-not-valid
>
*/
> serr.$chkeys
[ 'succ$0', 'area$1', 'birth$2', 'sex$3', 'police$4', 'parity$5' ]
>
>
> serr.area.$get_full_chkey(1)
'province$1'
>
add delete
> serr.$del('parity')
{
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
>
serr.$add(parity,'parity$0043')
/*
{
'succ$0': 'success',
'area$1': {
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
},
'birth$2': {
'succ$0': 'success',
'length$1': 'length-must-be-8',
'year$2': 'must-be-human',
'month$3': 'month-must-between-01-12',
'day$4': 'day-must-between-01-28,29,30,31'
},
'sex$3': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-1,3,5,7,9(man)-or-0,2,4,6,8(woman)'
},
'police$4': {
'succ$0': 'success',
'length$1': 'length-must-be-2',
'value$2': 'value-must-be-^[0-9]{2}$'
},
'parity$0043': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
}
*/
serr.$del('0043')
serr.$plset(['parity$5'],parity)
/*
> serr
{
'succ$0': 'success',
'area$1': {
'succ$0': 'success',
'province$1': 'province-not-valid',
'city$2': 'city-not-valid',
'county$3': 'county-not-valid',
'length$4': 'length-must-be-6'
},
'birth$2': {
'succ$0': 'success',
'length$1': 'length-must-be-8',
'year$2': 'must-be-human',
'month$3': 'month-must-between-01-12',
'day$4': 'day-must-between-01-28,29,30,31'
},
'sex$3': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-1,3,5,7,9(man)-or-0,2,4,6,8(woman)'
},
'police$4': {
'succ$0': 'success',
'length$1': 'length-must-be-2',
'value$2': 'value-must-be-^[0-9]{2}$'
},
'parity$5': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
}
>
*/
change key
serr.$add_key('parity','verify$validate')
/*
> serr
{
......
'parity$5$verify$validate': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
}
>
*/
serr.$del_key('parity','verify$validate')
/*
....
'parity$5': {
'succ$0': 'success',
'length$1': 'length-must-be-1',
'value$2': 'value-must-be-^[0-9X]$',
'parity$3': 'parity-error'
}
}
*/
METHODS
LICENSE
- ISC