@jocam/js
v1.0.0
Published
Javascript tools
Readme
@jocam/js
Javascript utility library used across @jocam projects.
TOC
const { clone } = require('@jocam/js');
//or
const clone = require('@jocam/js/clone'); //lodash way (less memory consumption)> const clone = require('@jocam/js/clone');
undefined
> const a = { a: 1}
undefined
> const b = clone(a)
undefined
> a === b
falseremoveNulls
Creates a copy of the object removing the null values in the process
Parameters
Examples
const removeNulls = require('@jocam/js/remove-nulls');
// or
const { removeNulls } = require('@jocam/js');
// or (if you are in frontend)
import removeNulls from '@jocam/is';
removeNulls({ a: null }); // {}
removeNulls({ a: { b: null } }); // {}
removeNulls({ a: 'a', b: null }); // { a: 'a' }
removeNulls({ a: 'a', b: { c: 'c', d: null } }); // { a: 'a', b: { c: 'c' }}Returns object
Type validations
checkType
returns the type of value
Parameters:
valueany value to check
Examples:
const { checkType } = require('@jocam/js');
//lodash style (reduce memory consumption)
const checkType = require('@jocam/js/checkType');
// or if you are in frontend:
import { checkType } from '@jocam/js';
console.log(check(1)); // number
console.log(check(false)); // boolean
console.log(check('1')); // stringReturns string
isBoolean
check if value type is boolean
Parameters:
valueboolean value to check
Examples:
const { isBoolean } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isBoolean = require('@jocam/js/isBoolean');
// or if you are in frontend:
import { isBoolean } from '@jocam/js';
console.log(isBoolean(0)); // false
console.log(isBoolean(1)); // false
console.log(isBoolean(true)); // true
console.log(isBoolean(false)); // true
console.log(isBoolean(!!0)); // trueReturns boolean
isString
check if value type is string
Parameters:
valuestring value to check
Examples:
const { isString } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isString = require('@jocam/js/isString');
// or if you are in frontend:
import { isString } from '@jocam/js';
console.log(isString(0)); // false
console.log(isString(1)); // false
console.log(isString('true')); // true
console.log(isString('false')); // trueReturns boolean
isDate
check if value type is date
Parameters:
valuedate value to check
Examples:
const { isDate } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isDate = require('@jocam/js/isDate');
// or if you are in frontend:
import { isDate } from '@jocam/js';
const today = new Date();
const now = Date.now();
console.log(isDate()); // false
console.log(isDate(now)); // false
console.log(isDate(today)); // trueReturns boolean
isNumber
check if value type is number
Parameters:
valuenumber value to check
Examples:
const { isNumber } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isNumber = require('@jocam/js/isNumber');
// or if you are in frontend:
import { isNumber } from '@jocam/js';
const now = Date.now();
console.log(isNumber(!!0)); // false
console.log(isNumber('pepe')); // false
console.log(isNumber(0)); // true
console.log(isNumber(now)); // trueReturns boolean
isUndefined
check if value is undefined
Parameters:
valueundefined value to check
Examples:
const { isUndefined } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isUndefined = require('@jocam/js/isUndefined');
// or if you are in frontend:
import { isUndefined } from '@jocam/js';
console.log(isUndefined()); // true
console.log(isUndefined(undefined)); // true
console.log(isUndefined(null)); // falseReturns boolean
isNull
check if value is null
Parameters:
valuenull value to check
Examples:
const { isNull } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isNull = require('@jocam/js/isNull');
// or if you are in frontend:
import { isNull } from '@jocam/js';
console.log(isNull()); // false
console.log(isNull(undefined)); // false
console.log(isNull(null)); // trueReturns boolean
isObject
check if value type is object
Parameters:
valueobject value to check
Examples:
const { isObject } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isObject = require('@jocam/js/isObject');
// or if you are in frontend:
import { isObject } from '@jocam/js';
console.log(isObject(0)); // false
console.log(isObject({ a: 1 })); // true
console.log(isObject({})); // trueReturns boolean
isArray
check if value type is array
Parameters:
valuearray value to check
Examples:
const { isArray } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isArray = require('@jocam/js/isArray');
// or if you are in frontend:
import { isArray } from '@jocam/js';
console.log(isArray('pepe')); // false
console.log(isArray(['pepe'])); // true
console.log(isArray([])); // trueReturns boolean
isError
check if value type is error
Parameters:
valueerror value to check
Examples:
const { isError } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isError = require('@jocam/js/isError');
// or if you are in frontend:
import { isError } from '@jocam/js';
const error = new Error('pepe');
console.log(isError()); // false
console.log(isError({ code: 'A', message: 'B' })); // false
console.log(isError(error)); // trueReturns boolean
isFunction
check if value type is function
Parameters:
valueFunction value to check
Examples:
const { isFunction } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isFunction = require('@jocam/js/isFunction');
// or if you are in frontend:
import { isFunction } from '@jocam/js';
console.log(isFunction(a => a)); // trueReturns boolean
isEmpty
check if value is an empty string, object or array
Parameters:
Examples:
const { isEmpty } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isEmpty = require('@jocam/js/isEmpty');
// or if you are in frontend:
import { isEmpty } from '@jocam/js';
console.log(isEmpty()); // false
console.log(isEmpty({ a: 1 })); // false
console.log(isEmpty('hi!')); // false
console.log(isEmpty([1, 2])); // false
console.log(isEmpty({})); // true
console.log(isEmpty('')); // true
console.log(isEmpty([])); // trueReturns boolean
isRegExp
check if value type is a Regular Expression
Parameters:
valueRegExp value to check
Examples:
const { isRegExp } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isRegExp = require('@jocam/js/isRegExp');
// or if you are in frontend:
import { isRegExp } from '@jocam/js';Returns boolean
isSymbol
check if value type is Symbol
:Parameters
valueSymbol value to check
Examples:
const { isSymbol } = equire('@jocam/js');
//lodash style (reduce memory consumption)
const isSymbol = require('@jocam/js/isSymbol');
// or if you are in frontend:
import { isSymbol } from '@jocam/js';Returns boolean
