BellaJS
Lightweight util for handling data type, string... in your Node.js and browser apps.
Contents
-
Setup
-
APIs
- DataType detection
- String manipulation
- Data handling:
clone
, copies
- Array utils:
pick
, sort
, sortBy
, shuffle
, unique
- Functional utils:
curry
, compose
, pipe
, maybe
- Date utils:
formatDateString
, formatTimeAgo
- Random utils:
randint
, genid
-
Test
-
License
Install & Usage
Node.js
npm i bellajs
pnpm i bellajs
yarn add bellajs
Deno
import { genid } from 'https://esm.sh/bellajs'
console.log(genid())
Browser
<script type="module">
import { genid, slugify } from 'https://unpkg.com/bellajs/dist/bella.esm.js'
console.log(genid())
</script>
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)
Data handling
clone(Anything val)
Make a deep copy of a variable.
import { clone } from 'bellajs'
const b = [
1, 5, 0, 'a', -10, '-10', '',
{
a: 1,
b: 'Awesome'
}
]
const cb = 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'
}
copies(Object source, Object target[[, Boolean requireMatching], Array excepts])
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.
After this action, target will be modified.
import { copies } from 'bellajs'
const a = {
name: 'Toto',
age: 30,
level: 8,
nationality: {
name: 'America'
}
}
const b = {
level: 4,
IQ: 140,
epouse: {
name: 'Alice',
age: 27
},
nationality: {
long: '18123.123123.12312',
lat: '98984771.134231.1234'
}
}
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
}
Array utils
pick(Array arr [, Number count = 1])
Randomly choose N elements from array.
import { pick } from 'bellajs'
const arr = [1, 3, 8, 2, 5, 7]
pick(arr, 2)
pick(arr, 2)
pick(arr)
pick(arr)
sort(Array arr [, Function compare])
Sort the array using a function.
import { sort } from 'bellajs'
const fn = (a, b) => {
return a < b ? 1 : a > b ? -1 : 0
}
sort([3, 1, 5, 2], fn)
sortBy(Array arr, Number order, String property)
Sort the array by specific property and direction.
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(players, -1, 'age')
console.log(result)
shuffle(Array arr)
Shuffle the positions of elements in an array.
import { shuffle } from 'bellajs'
shuffle([1, 3, 8, 2, 5, 7])
unique(Array arr)
Remove all duplicate elements from an array.
import { unique } from 'bellajs'
unique([1, 2, 3, 2, 3, 1, 5])
Functional utils
curry(fn)
Make a curried function.
import { curry } from 'bellajs'
const 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)
compose(f1, f2, ...fN)
Performs right-to-left function composition.
import { compose } from 'bellajs'
const f1 = (name) => {
return `f1 ${name}`
}
const f2 = (name) => {
return `f2 ${name}`
}
const f3 = (name) => {
return `f3 ${name}`
}
const addF = compose(f1, f2, f3)
addF('Hello')
const add1 = (num) => {
return num + 1
}
const mult2 = (num) => {
return num * 2
}
const add1AndMult2 = compose(add1, mult2)
add1AndMult2(3)
pipe(f1, f2, ...fN)
Performs left-to-right function composition.
import { pipe } from 'bellajs'
const f1 = (name) => {
return `f1 ${name}`
}
const f2 = (name) => {
return `f2 ${name}`
}
const f3 = (name) => {
return `f3 ${name}`
}
const addF = pipe(f1, f2, f3)
addF('Hello')
const add1 = (num) => {
return num + 1
}
const mult2 = (num) => {
return num * 2
}
const add1AndMult2 = pipe(add1, mult2)
add1AndMult2(3)
maybe(Anything val)
Return a static variant of Maybe
monad.
import { maybe } from 'bellajs'
const plus5 = x => x + 5
const minus2 = x => x - 2
const isNumber = x => Number(x) === x
const toString = x => 'The value is ' + String(x)
const getDefault = () => 'This is default value'
maybe(5)
.map(plus5)
.map(minus2)
.value()
maybe('noop')
.map(plus5)
.map(minus2)
.value()
maybe(5)
.if(isNumber)
.map(plus5)
.map(minus2)
.else(getDefault)
.map(toString)
.value()
maybe()
.if(isNumber)
.map(plus5)
.map(minus2)
.map(toString)
.value()
maybe()
.if(isNumber)
.map(plus5)
.map(minus2)
.else(getDefault)
.map(toString)
.value()
Date utils
formatDateString(Date | Timestamp [, String locale [, Object options]])
import {
formatDateString
} from 'bellajs'
const today = new Date()
formatDateString(today)
formatDateString(today, {
dateStyle: 'short',
timeStyle: 'short',
hour12: true
})
formatDateString(today, 'zh')
formatDateString(today, 'zh', {
dateStyle: 'short',
timeStyle: 'long',
hour12: true
})
formatDateString(today, 'vi')
formatDateString(today, 'vi', {
dateStyle: 'full',
timeStyle: 'full'
})
formatTimeAgo(Date | Timestamp [, String locale [, String justnow]])
import {
formatTimeAgo
} from 'bellajs'
const today = new Date()
const yesterday = today.setDate(today.getDate() - 1)
formatTimeAgo(yesterday)
const current = new Date()
const aLittleWhile = current.setHours(current.getHours() - 3)
formatTimeAgo(aLittleWhile)
formatTimeAgo(aLittleWhile, 'zh')
formatTimeAgo(aLittleWhile, 'vi')
The last param justnow
can be used to display a custom 'just now' message, when the distance is lesser than 1s.
const now = new Date()
const aJiff = now.setTime(now.getTime() - 100)
formatTimeAgo(aJiff)
formatTimeAgo(aJiff, 'fr', 'à l\'instant')
formatTimeAgo(aJiff, 'ja', 'すこし前')
These two functions based on recent features of built-in object Intl
.
Please refer the following resources for more info:
Random utils
randint([Number min [, Number max]])
Returns a number between min
and max
import { randint } from 'bellajs'
randint()
randint(1, 5)
genid([Number length [, String prefix]])
Create random ID string.
import { genid } from 'bellajs'
genid()
genid(16)
genid(5)
genid(5, 'X_')
Test
git clone https://github.com/ndaidong/bellajs.git
cd bellajs
npm install
npm test
License
The MIT License (MIT)