@acastellon/utils
v1.2.0
Published
Common function utils for javascript
Downloads
68
Maintainers
Readme
@acastellon/utils
Minor common utils for javascript.
Install
npm install @acastellon/utilsUsage
const utils = require('@acastellon/utils')();
if (utils.isNotEmpty(someValue)) { ... }
if (utils.isAnySQLInjection(userInput)) { throw new Error('injection'); }
console.log( utils.existProperty(foo) );API
All functions are attached to the returned model.
existProperty(prop: any): boolean
Returns true if prop is non-null, defined, and either a non-empty string or a number.
Example:
const utils = require('@acastellon/utils')();
console.log(utils.existProperty('hello')); // true
const config = { host: 'localhost' };
console.log(utils.existProperty(config.host)); // true
console.log(utils.existProperty('')); // falseisNotNull(data: any): boolean
Alias-ish for defined + not empty.
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isNotNull('value')); // true
console.log(utils.isNotNull(null)); // falseisEmpty(data: any): boolean
Negation of isNotEmpty.
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isEmpty('')); // true
console.log(utils.isEmpty({})); // trueisNotEmpty(data: any): boolean
True for non-undefined/non-null values that are either non-empty objects (keys) or have string length >0.
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isNotEmpty({a:1})); // true
console.log(utils.isNotEmpty([])); // false
console.log(utils.isNotEmpty(42)); // trueisNotUndefined(data: any): boolean
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isNotUndefined(0)); // true
console.log(utils.isNotUndefined(undefined)); // falseisUndefined(data: any): boolean
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isUndefined(undefined)); // trueisAnObject(data: any): boolean
typeof === 'object' (arrays and null-ish are true here).
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isAnObject({})); // true
console.log(utils.isAnObject('string')); // falseisAnySQLInjection(data: any): boolean
Checks for presence of UPDATE/DROP/DELETE/INSERT/CREATE (only on strings).
Example:
const utils = require('@acastellon/utils')();
console.log(utils.isAnySQLInjection('DROP TABLE users')); // true
console.log(utils.isAnySQLInjection('SELECT * FROM x')); // false
console.log(utils.isAnySQLInjection(123)); // falseLicense
MIT
