Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Lightweight util for handling data type, string... in your Node.js and browser apps.
You may be interested in BellaPy too.
Node.js
npm i bellajs
CDN
Load with ESM, CommonJS, AMD or UMD style
const bella = require('bellajs');
// few methods only:
const {
isArray,
isString,
} = require('bellajs');
// es6 syntax:
import bella from 'bellajs';
// or
import {
isArray,
isString,
} from 'bellajs';
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
Example:
import {
toRelativeTime,
toDateString,
toLocalDateString,
toUTCDateString
} from 'bellajs'
const t = 1509628030108
toRelativeTime(t) // => 2 seconds ago
toDateString(t, 'Y/m/d h:i:s') // => 2017/11/02 20:07:10
toLocalDateString(t) // => Thu, 2 Nov 2017 20:07:10 GMT+0007
toUTCDateString(t) // => Thu, 2 Nov 2017 13:07:10 GMT+0000
clone(Anything val)
Return a copy of val.
const b = [
1, 5, 0, 'a', -10, '-10', '',
{
a: 1,
b: 'Awesome'
}
]
const 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'
}
Copy the properties from source to target.
copies(Object source, Object target[[, Boolean requireMatching], Array excepts])
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'
}
}
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
}
Randomly choose N elements from array.
pick(Array arr [, Integer count = 1])
Examples:
import { pick } from 'bellajs'
const arr = [1, 3, 8, 2, 5, 7]
pick(arr, 2) // --> [3, 5]
pick(arr, 2) // --> [8, 1]
pick(arr) // --> [3]
pick(arr) // --> [7]
sort(Array a [, Function compare])
For example:
import { sort } from 'bellajs'
const fn = (a, b) => {
return a < b ? 1 : a > b ? -1 : 0
}
sort([3, 1, 5, 2], fn) // => [ 1, 2, 3, 5 ]
sortBy(Array a, Number order, String property)
For example:
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 an array.
shuffle(Array arr)
For example:
import { shuffle } from 'bellajs'
shuffle([1, 3, 8, 2, 5, 7])
unique(Array a)
For example:
import { unique } from 'bellajs'
unique([1, 2, 3, 2, 3, 1, 5]) // => [ 1, 2, 3, 5 ]
curry(fn)
Examples:
import { curry } from 'bellajs'
const sum = curry((a, b, c) => {
return a + b + c
})
sum(3)(2)(1) // => 6
sum(1)(2)(3) // => 6
sum(1, 2)(3) // => 6
sum(1)(2, 3) // => 6
sum(1, 2, 3) // => 6
Performs right-to-left function composition.
compose(f1, f2, ...fN)
Examples:
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') // => 'f1 f2 f3 Hello'
const add1 = (num) => {
return num + 1
}
const mult2 = (num) => {
return num * 2
}
const add1AndMult2 = compose(add1, mult2)
add1AndMult2(3) // => 7
// because multiple to 2 first, then add 1 late => 3 * 2 + 1
Performs left-to-right function composition.
pipe(f1, f2, ...fN)
Examples:
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') // => 'f3 f2 f1 Hello'
const add1 = (num) => {
return num + 1
}
const mult2 = (num) => {
return num * 2
}
const add1AndMult2 = pipe(add1, mult2)
add1AndMult2(3) // => 8
// because add 1 first, then multiple to 2 late => (3 + 1) * 2
maybe(Anything val)
Return a static variant of Maybe
monad.
Examples:
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() // 8
maybe('noop')
.map(plus5)
.map(minus2)
.value() // null
maybe(5)
.if(isNumber)
.map(plus5)
.map(minus2)
.else(getDefault)
.map(toString)
.value() // 'The value is 8'
maybe()
.if(isNumber)
.map(plus5)
.map(minus2)
.map(toString)
.value() // null
maybe()
.if(isNumber)
.map(plus5)
.map(minus2)
.else(getDefault)
.map(toString)
.value() // 'This is default value'
equals(Anything a, Anything b)
Examples:
import { equals } from 'bellajs'
equals({}, {}) // => true
equals(0, 1) // => false
randint([Number min [, Number max]])
Examples:
import { randint } from 'bellajs'
randint() // => a random integer
randint(1, 5) // => a random integer between 3 and 5, including 1 and 5
genid([Number length [, String prefix]])
Examples:
import { genid } from 'bellajs'
genid() // => random 32 chars
genid(16) // => random 16 chars
genid(5) // => random 5 chars
genid(5, 'X_') // => X_{random 3 chars}
md5(String s)
For example:
import { md5 } from 'bellajs'
md5('abc') // => 900150983cd24fb0d6963f7d28e17f72
git clone https://github.com/ndaidong/bellajs.git
cd bellajs
npm install
npm test
The MIT License (MIT)
FAQs
A useful helper for any javascript program
We found that bellajs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.