celia
Note: A modern JavaScript utility library delivering modularity, performance, and extras.
Installation
In a browser
<script src="https://cdn.jsdelivr.net/npm/celia/celia.min.js"></script>
<script>
celia.each
celia.isArrayLike
celia.isAsyncFunction
celia.isDate
celia.isFalsy
celia.looseEqual
celia.sleep
</script>
Using npm
npm install celia --save
import { assign, each, getUid, hasOwn, isArrayLike, isUndefined, isWindow, looseEqual, map, noop, sleep, toString, type } from 'celia');
import each from 'celia/each';
import isArrayLike from 'celia/isArrayLike';
import isAsyncFunction from 'celia/isAsyncFunction';
import isDate from 'celia/isDate';
import isFalsy from 'celia/isFalsy';
import looseEqual from 'celia/looseEqual';
import sleep from 'celia/sleep';
const { assign, each, getUid, hasOwn, isArrayLike, isUndefined, isWindow, looseEqual, map, noop, sleep, toString, type } = require('celia');
const each = require('celia/each');
const isArrayLike = require('celia/isArrayLike');
const isAsyncFunction = require('celia/isAsyncFunction');
const isDate = require('celia/isDate');
const isFalsy = require('celia/isFalsy');
const looseEqual = require('celia/looseEqual');
const sleep = require('celia/sleep');
API
assert
-
assert(condition, msg)
condition
<Boolean>
msg
<String>
error message
-
Throws
<Error>
throws Error if condition
is false
assert(1 !== 1, 'assertion error');
each
- each(value, callback[, context])
value
<Array|ArrayLike|String|Number|Object>
callback
<Function>
context
<any>
optional
each([1, 2], (val, index)=> {
})
each({ a: 1, b: 2 }, (val, key)=> {
})
each(5, (index, index)=> {
})
each('hello', (c, index)=> {
})
easyHash
-
easyHash(value)
-
Returns
<String>
returns an easy hash value
easyHash('abc')
easyHash(1)
easyHash({})
hasOwn
const obj = { aaa: 111 };
hasOwn(obj, 'test')
hasOwn(obj, 'aaa')
looseClone
-
looseClone(value)
-
Returns
const a = { a: 1 };
const b = looseClone(a);
looseEqual
looseEqual(null, undefined)
looseEqual(null, null)
looseEqual({ a: 1, b: 2 }, { a: 1, b: 2 })
looseEqual([1, 2], [1, 2])
looseEqual(/\d+/, /\d+/)
looseEqual(new Date(2019, 0, 1, 9, 9, 9), new Date(2019, 0, 1, 9, 9, 9))
noop
noop()
sleep
-
sleep(value)
value
<Number>
milliseconds
-
Returns
async function foo() {
await sleep(3000);
}
toString
toString({})
toString([])
isAbsoluteURL
-
isAbsoluteURL(url)
url
<String>
a request url
-
Returns
<Boolean>
returns true if the url
is a absolute request url
isAbsoluteURL('/src/isAbsoluteURL.js'))
isAbsoluteURL('https://github.com'))
isArrayLike
-
isArrayLike(value)
-
Returns
isArrayLike('123')
isArrayLike(() => { })
isArrayLike([])
isArrayLike([1, 2, 3])
isArrayLike({ 0: 1, length: 1 })
isAsyncFunction
-
isAsyncFunction(value)
-
Returns
isAsyncFunction(async () => { })
isAsyncFunction(() => { })
isBoolean
isBoolean(() => { })
isBoolean(true)
isDate
isDate(new Date())
isDate({})
isFalsy
isFalsy(false)
isFalsy(null)
isFalsy(undefined)
isFalsy(0)
isFalsy(NaN)
isFalsy('')
isFalsy({})
isFunction
-
isFunction(value)
-
Returns
isFunction(async () => { })
isFunction(() => { })
isFunction({})
isInteger
isInteger(2)
isInteger(-2)
isInteger(1.23)
isInteger(-1.23)
isInteger(null)
isInteger(undefined)
isInteger('2'));
isInteger(Infinity)
isLeapYear
-
isLeapYear(value)
-
Returns
isLeapYear(1997)
isNil
isNil(null)
isNil(undefined)
isNil({})
isNumber
isNumber(1)
isNumber(undefined)
isNumber({})
isObject
isObject(1)
isObject(undefined)
isObject({})
isPlainObject
-
isPlainObject(value)
-
Returns
isPlainObject(new Date())
isPlainObject({})
isPromiseLike
-
isPromiseLike(value)
-
Returns
isPromiseLike(null)
isPromiseLike(undefined)
isPromiseLike({})
isPromiseLike(new Promise(() => { }))
isPromiseLike({ then: () => { }, catch: () => { } })
isRegExp
isRegExp(null)
isRegExp(undefined)
isRegExp({})
isRegExp(Object.create(null))
isRegExp(/\d+/)
isString
isString(null)
isString({})
isString(1)
isString(true)
isString('')
isUndefined
-
isUndefined(value)
-
Returns
isUndefined(null)
isUndefined(undefined)
isValidDate
-
isValidDate(value)
-
Returns
isValidDate(null)
isValidDate(undefined)
isValidDate(new Date(NaN))
isValidDate(new Date())
isWindow
isWindow(null)
isWindow(undefined)
isWindow(window)
Collection Methods
map
map([1, 2, 3, 4, 5], n => n + 1)
map([1, null, 2, undefined, 3, 4, 5], n => n && (n + 1))
map({ a: 1, b: 2 }, n => n && (n + 1))
transform
const obj = { a: 1, b: 2 };
const newObj = transform(obj, (newObj, value, key) => {
newObj[key] = value + 1;
}, {});
const arr = [1, 2];
const newArr = transform(arr, (newArr, value, key) => {
newArr[key] = value + 1;
}, []);
uid
uid({})
uid({})
alias
alias({ a: 1, b: 2 }, {
a: 'c',
b: ['e', 'f']
});
assign
const a = { a: 1 };
const b = { b: 2 };
const c = { c: 3 };
assign(a, b, c);
deepAssign
const a = { a: 1 };
const b = { b: {bb: 11} };
deepAssign(a, b);
forIn
- forIn(value, callback[, context])
value
<Object>
callback
<Function>
context
<any>
optional
forIn({ a: 1, b: 2 }, (val, key) => {
});
forOwn
- forOwn(value, callback[, context])
value
<Object>
callback
<Function>
context
<any>
optional
forOwn({ a: 1, b: 2 }, (val, key) => {
});
get
const a = { a: [{ b: { c: 3 } }], key: 'value' };
get(null, 'a[0].b.c', 3)
get(a, 'a[0].b.c')
get(a, 'a["0"].b.c')
get(a, 'a.b.c')
get(a, 'a.b.c', 'default')
get(a, 'key')
get({ key: null }, 'key')
set
- set(object, path, value)
object
<Object>
path
<String>
value
<any>
const a = { a: [{ b: { c: 3 } }], key: 'value' };
set(a, 'a[0].b.c', 1);
deepFlat
const arr1 = [1, [2], [], 3, 4, 5];
deepFlat(arr1)
const arr2 = [1, [2, [1, 2, [2, 3]], 3], [], 3, [[1, 2], [[1, 2, 3], 3], [1, 2]], 4, 5];
deepFlat(arr2)
flat
-
flat(arr[, depth])
arr
<Array>
depth
<Number>
Default: 1
-
flat
const arr1 = [1, [2], [], 3, 4, 5];
flat(arr1)
const arr2 = [1, 2, 3, [2, 3, 4], [[1, 2, 3], [3, 4, 5]], 1, 3, 4];
flat(arr2)
forEach
-
forEach(arr, callback[, context])
arr
<Array>
callback
<Function>
context
<any>
Optional
-
forEach(arr, start, callback[, context])
arr
<Array>
start
<Number>
callback
<Function>
context
<any>
Optional
-
forEach(arr, start, end, callback[, context])
arr
<Array>
start
<Number>
end
<Number>
callback
<Function>
context
<any>
Optional
let i = 0;
forEach([1, 2, 3], function(num) {
if (num === 1) {
return false;
}
i++;
});
i = 0;
forEach([1, 2, 3, 4, 5], 1, function(num) {
if (num === 3) {
return false;
}
i++;
});
i = 0;
forEach([1, 2, 3, 4, 5], 1, -1, function(num) {
i++;
});
remove
-
remove(arr, value)
-
Returns
<any>
returns something being removed
const arr = [1, 2, 3, 4, 5];
remove(arr, 2)
remove(arr, 9)
removeAt
-
removeAt(arr, index)
arr
<Array>
index
<Number>
-
Returns
<any>
returns something being removed
const arr = [1, 2, 3, 4, 5];
removeAt(arr, 2)
removeAt(arr, 9)
camelize
camelize('-value')
camelize('data-value')
camelize('data-------value')
camelize('data-attr-value')
camelize('data--attr--value')
camelize('data_value')
camelize('data_______value')
camelize('data_attr_value')
camelize('data__attr__value')
camelize('data value')
camelize('data value')
camelize('data attr value')
camelize('data attr value')
camelize('data.value')
camelize('data.......value')
camelize('data.attr.value')
camelize('data..attr..value')
capitalize
-
capitalize(value)
-
Returns
capitalize('value')
pathJoin
- pathJoin(base, arg[, arg2, arg3])
base
<String>
arg
<String|Number|Boolean>
pathJoin('https://www.baidu.com', 'path1')
pathJoin('https://www.baidu.com/', 'path1')
pathJoin('https://www.baidu.com', 'path1', 'path2)
// => 'https:
stringFormat
stringFormat('共{ page }条记录', {})
stringFormat('共{ page }条记录', { page: 2 })
afterCall
- afterCall(obj, methodName, after)
obj
<Object>
methodName
<String>
after
<Function>
const obj = {
counter: 2,
increment() {
this.counter++;
}
};
afterCall(obj, 'increment', function (args, ret) {
this.counter = 1;
});
obj.increment();
aroundCall
- aroundCall(obj, methodName, after)
obj
<Object>
methodName
<String>
after
<Function>
const obj = {
counter: 2,
increment() {
this.counter++;
}
};
aroundCall(obj, 'increment', function (args, fn) {
this.counter = 2;
fn.apply(this, args);
this.counter = 2;
});
obj.increment();
beforeCall
- beforeCall(obj, methodName, after)
obj
<Object>
methodName
<String>
after
<Function>
const obj = {
counter: 2,
increment() {
this.counter++;
}
};
beforeCall(obj, 'increment', function () {
this.counter = 1;
});
obj.increment();
debounce
-
debounce(func, wait)
func
<Function>
wait
<Number>
-
Returns
async function() {
let i = 0;
const counter = debounce(() => {
i++;
}, 200);
counter();
await sleep(100);
counter();
await sleep(100);
counter();
await sleep(200);
}