1.1.1 • Published 6 years ago
logic-emitter v1.1.1
npm install -S logic-emitter
const { hit,target,equal } = require('logic-emitter')
const Harry = {
name: 'Harry',
age: 23,
wife:{
name: 'Marry',
age: 20,
son:{
name: 'Heey',
age: 3
}
},
son:{
name: 'Heey',
age: 3
}
}
console.log(
hit(Harry,'wife','name'), // true
hit(Harry,'son','name'), // true
hit(Harry,'son','wife'), // false
hit(Harry,'son','wife','name'), // false
target(Harry,'son','name'), // 'Heey'
target(Harry,'wife','name'), // 'Marry'
equal(Harry,'wife','name','Marry'), // true
equal(Harry,'wife','name','John') // false
)
It is terrible do the logical in a long attribute chain
if(Harry && Harry.son && Harry.son.name) {
console.log(Harry.son.name)
}
You can do it like this
if(hit(Harry,'son','name')) {
console.log(Harry.son.name)
}
or
console.log(target(Harry,'son','name'))