BellaJS
Lightweight util for handling data type, string... in your Node.js and browser apps.
You may be interested in BellaPy too.
Contents
Setup
Usage
const bella = require('bellajs');
const {
isArray,
isString,
} = require('bellajs');
import bella from 'bellajs';
import {
isArray,
isString,
} from 'bellajs';
APIs
DataType detection
- .isArray(Anything val)
- .isBoolean(Anything val)
- .isDate(Anything val)
- .isElement(Anything val)
- .isEmail(Anything val)
- .isEmpty(Anything val)
- .isFunction(Anything val)
- .isInteger(Anything val)
- .isLetter(Anything val)
- .isNull(Anything val)
- .isNumber(Anything val)
- .isObject(Anything val)
- .isString(Anything val)
- .isUndefined(Anything val)
String manipulation
- .ucfirst(String s)
- .ucwords(String s)
- .escapeHTML(String s)
- .unescapeHTML(String s)
- .slugify(String s)
- .stripTags(String s)
- .stripAccent(String s)
- .truncate(String s, Number limit)
- .replaceAll(String s, String|Array search, String|Array replace)
Date format
relativize([Date | Timestamp])
format([Date | Timestamp] [, String pattern])
local([Date | Timestamp])
utc([Date | Timestamp])
Default pattern for format()
method is D, M d, Y H:i:s A
.
Pattern for local()
and utc()
is D, j M Y h:i:s O
.
Here are the available characters:
- Y: full year, ex: 2050
- y: short year, ex: 50
- F: full month name, ex: August
- M: short month name, ex: Aug
- m: month index with zero, ex: 08 (in 08/24/2050)
- n: short month name with no zero, ex: 8 (in 8/24/2050)
- S: the ordering subfix for date, ext: 1st, 2nd, 3rd, 4th
- j: day of the month, with no zero, ex: 3 (in 18/3/2050)
- d: day of the month, with zero, ex: 03 (in 18/03/2050)
- t: date in year
- w: weekday in number
- l: long name of weekday, ex: Sunday
- D: short name of weekday, ex: Sun
- G: hour, with no zero: 0 - 24
- g: hour, with no zero: 0 - 12
- h: hour, with zero: 00 - 24
- i: minute: 00 - 59
- s: second: 00 - 59
- a: am, pm
- A: AM, PM
- O: timezone
t s
Example:
import {
relativize,
format,
local,
utc
} from 'bellajs';
let t = 1509628030108;
relativize(t);
format(t, 'Y/m/d h:i:s');
local(t);
utc(t);
Other utils
clone
clone(Anything val)
Return a copy of val.
let b = [
1, 5, 0, 'a', -10, '-10', '',
{
a: 1,
b: 'Awesome'
}
];
let cb = bella.clone(b);
console.log(cb);
cb now has the same values as b, while the properties are standalone, not reference. So that:
cb[7].a = 2;
cb[7].b = 'Noop';
console.log(b[7]);
What you get is still:
{
a: 1,
b: 'Awesome'
}
compose
Performs right-to-left function composition.
compose(f1, f2, ...fN)
Examples:
import {compose} from 'bellajs';
let f1 = (name) => {
return `f1 ${name}`;
};
let f2 = (name) => {
return `f2 ${name}`;
};
let f3 = (name) => {
return `f3 ${name}`;
};
let addF = compose(f1, f2, f3);
addF('Hello')
let add1 = (num) => {
return num + 1;
};
let mult2 = (num) => {
return num * 2;
};
let add1AndMult2 = compose(add1, mult2);
add1AndMult2(3)
copies
Copy the properties from source to target.
copies(Object source, Object target[[, Boolean requireMatching], Array excepts])
- requireMatching: if true, BellaJS only copies the properties that are already exist in target.
- excepts: array of the properties properties in source that you don't want to copy.
Example:
let a = {
name: 'Toto',
age: 30,
level: 8,
nationality: {
name: 'America'
}
};
let b = {
level: 4,
IQ: 140,
epouse: {
name: 'Alice',
age: 27
},
nationality: {
long: '18123.123123.12312',
lat: '98984771.134231.1234'
}
};
bella.copies(a, b);
console.log(b);
Output:
{
level: 8,
IQ: 140,
epouse: {
name: 'Alice',
age: 27
},
nationality: {
long: '18123.123123.12312',
lat: '98984771.134231.1234',
name: 'America'
},
name: 'Toto',
age: 30
}
curry
curry(fn)
Examples:
import {curry} from 'bellajs';
let sum = curry((a, b, c) => {
return a + b + c;
});
sum(3)(2)(1)
sum(1)(2)(3)
sum(1, 2)(3)
sum(1)(2, 3)
sum(1, 2, 3)
equals
equals(Anything a, Anything b)
Examples:
import {equals} from 'bellajs';
equals({}, {});
equals(0, 1);
genid
genid([Number length [, String prefix]])
Examples:
import {genid} from 'bellajs';
genid();
genid(16);
genid(5);
genid(5, 'X_');
md5
md5(String s)
Examples:
import {md5} from 'bellajs';
md5('abc');
pick
Randomly choose N elements from array.
pick(Integer count, Array arr)
Examples:
import {pick} from 'bellajs';
const arr = [1, 3, 8, 2, 5, 7]
pick(arr, 2);
pick(arr, 2);
pipe
Performs left-to-right function composition.
pipe(f1, f2, ...fN)
Examples:
import {pipe} from 'bellajs';
let f1 = (name) => {
return `f1 ${name}`;
};
let f2 = (name) => {
return `f2 ${name}`;
};
let f3 = (name) => {
return `f3 ${name}`;
};
let addF = pipe(f1, f2, f3);
addF('Hello')
let add1 = (num) => {
return num + 1;
};
let mult2 = (num) => {
return num * 2;
};
let add1AndMult2 = pipe(add1, mult2);
add1AndMult2(3)
randint
randint([Number min [, Number max]])
Examples:
import {randint} from 'bellajs';
randint();
randint(1, 5);
sort
sort(Array a, Number order)
Examples:
import {sort} from 'bellajs';
sort([3, 1, 5, 2], 1);
sort([3, 1, 5, 2], -1);
sortBy
sortBy(Array a, String property, Number order)
Examples:
import {sortBy} from 'bellajs';
const players = [
{
name: 'Jerome Nash',
age: 24
},
{
name: 'Jackson Valdez',
age: 21
},
{
name: 'Benjamin Cole',
age: 23
},
{
name: 'Manuel Delgado',
age: 33
},
{
name: 'Caleb McKinney',
age: 28
}
];
const result = sortBy('age', -1, players);
console.log(result_)
shuffle
Shuffle an array.
shuffle(Array arr)
Examples:
import {shuffle} from 'bellajs';
shuffle([1, 3, 8, 2, 5, 7]);
unique
unique(Array a)
Examples:
import {unique} from 'bellajs';
unique([1, 2, 3, 2, 3, 1, 5]);
Test
git clone https://github.com/ndaidong/bellajs.git
cd bellajs
npm install
npm test
License
The MIT License (MIT)