BellaJS
Lightweight util for handling data type, string... in your Node.js and browser apps.
You may be interested in BellaPy too.
Contents
Setup
-
Node.js
npm i bellajs
-
CDN
-
Load with ESM, CommonJS, AMD or UMD style
Usage
const bella = ; // few methods only:const isArray isString = ; // es6 syntax:; // with tree shacking;
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)
- .isNil(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
toRelativeTime([Date | Timestamp])
toDateString([Date | Timestamp] [, String pattern])
toLocalDateString([Date | Timestamp])
toUTCDateString([Date | Timestamp])
Default pattern for toDateString()
method is D, M d, Y H:i:s A
.
Pattern for toLocalDateString()
and toUTCDateString()
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:
; let t = 1509628030108; ; //=> 2 seconds ago; //=> 2017/11/02 20:07:10; //=> Thu, 2 Nov 2017 20:07:10 GMT+0007; //=> Thu, 2 Nov 2017 13:07:10 GMT+0000
Other utils
clone
Return a copy of val.
let b = 1 5 0 'a' -10 '-10' '' a: 1 b: 'Awesome' ; let cb = bella;console;
cb now has the same values as b, while the properties are standalone, not reference. So that:
cb7a = 2;cb7b = 'Noop'; console;
What you get is still:
a: 1 b: 'Awesome'
compose
Performs right-to-left function composition.
Examples:
; let { return `f1 `;};let { return `f2 `;};let { return `f3 `;}; let addF = ; // => 'f1 f2 f3 Hello' let { return num + 1;}; let { return num * 2;}; let add1AndMult2 = ; // => 7// because multiple to 2 first, then add 1 late => 3 * 2 + 1
copies
Copy the properties from source to target.
- 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;console;
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
Examples:
; let sum = ; 21 // => 623 // => 63 // => 62 3 // => 6 // => 6
equals
Examples:
; ; // => true; // => false
genid
Examples:
; ; // => random 32 chars; // => random 16 chars; // => random 5 chars; // => X_{random 3 chars}
maybe
Return a static variant of Maybe
monad.
Examples:
; const plus5 = x + 5;const minus2 = x - 2;const isNumber = Numberx === x;const toString = 'The value is ' + Stringx;const getDefault = 'This is default value'; value // 8 value // null value // 'The value is 8' value // null value // 'This is default value'
md5
Examples:
; ; // => 900150983cd24fb0d6963f7d28e17f72
pick
Randomly choose N elements from array.
Examples:
; const arr = 1 3 8 2 5 7; // --> [3, 5]; // --> [8, 1]
pipe
Performs left-to-right function composition.
Examples:
; let { return `f1 `;};let { return `f2 `;};let { return `f3 `;}; let addF = ; // => 'f3 f2 f1 Hello' let { return num + 1;}; let { return num * 2;}; let add1AndMult2 = ; // => 8// because add 1 first, then multiple to 2 late => (3 + 1) * 2
randint
Examples:
; ; // => a random integer; // => a random integer between 3 and 5, including 1 and 5
sort
Examples:
; const fn = { return a < b ? 1 : a > b ? -1 : 0;}; ; // => [ 1, 2, 3, 5 ]; // => [ 5, 3, 2, 1 ]
sortBy
Examples:
; 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 = ;console
shuffle
Shuffle an array.
Examples:
; ;
unique
Examples:
; ; // => [ 1, 2, 3, 5 ]
Test
git clone https://github.com/ndaidong/bellajs.gitcd bellajsnpm installnpm test
License
The MIT License (MIT)