Provides some syntactic sugar and shortcuts to bullet proof your devs
npm install @jkhong/devutilsProvides some syntactic sugar and shortcuts to bullet proof your devs
js
import { DevUtils } from '@jkhong/devutils';const emptyArray = [];
const emptyObject = {};
const emptyString = '';
const values = [null, undefined, 'undefined', 'not empty', emptyString, emptyArray, emptyObject];
for (const val of values) {
if (DevUtils.isSet(val)) {
console.log(
value [${val}] is set);
} else {
console.log(value [${val}] is not set);
} // uses isSet + check string/array/object emptyness
if (DevUtils.isEmpty(val)) {
console.log(
value [${val}] is empty);
} else {
console.log(value [${val}] is not empty);
}
}
`returns an object field value or the default value
$3
- check if null or undefined or 'undefined' at once
- set actual value or a default one if not set
- value setting stays simple and human readable$3
`js
import { DevUtils } from '@jkhong/devutils';const emptyObject = {};
const defaultValue = 'myValue';
let objectFieldValue = DevUtils.getFieldValue(emptyObject, 'aField', defaultValue);
console.log(objectFieldValue); // prints myValue
const object = { aField: 'objectValue' };
objectFieldValue = DevUtils.getFieldValue(object, 'aField', defaultValue);
console.log(objectFieldValue); // prints objectValue
`returns an sub object field value or the default value
$3
- check if null or undefined or 'undefined' for each sub object
- set actual value or a default one if not set
- value setting stays simple and human readable$3
`js
import { DevUtils } from '@jkhong/devutils';const object = {
aSubObject: {
anotherSubObject: {
aField: 'objectValue',
},
},
};
const defaultValue = 'myValue';
const objectFieldValue = DevUtils.getChildFieldValue(
object,
['aSubObject', 'anotherSubObject', 'aField'],
defaultValue,
);
console.log(objectFieldValue); // prints objectValue
``