Installations
npm install @ndaidong/bellajs
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
20.18.0
NPM Version
10.8.2
Score
73.5
Supply Chain
98.9
Quality
77.9
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
Download Statistics
Total Downloads
669
Last Day
1
Last Week
2
Last Month
7
Last Year
669
GitHub Statistics
11 Stars
168 Commits
3 Forks
1 Watching
2 Branches
2 Contributors
Package Meta Information
Latest Version
12.0.1
Package Id
@ndaidong/bellajs@12.0.1
Unpacked Size
79.72 kB
Size
15.71 kB
File Count
137
NPM Version
10.8.2
Node Version
20.18.0
Publised On
26 Oct 2024
Total Downloads
Cumulative downloads
Total Downloads
669
Last day
0%
1
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
-92.4%
7
Compared to previous month
Last year
0%
669
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
3
BellaJS
Lightweight util for handling data type, string... in your Node.js and browser apps.
Contents
Setup & Usage
Deno
https://jsr.io/@ndaidong/bellajs
1import { genid } from "@ndaidong/bellajs"; 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
You can use JSR packages without an install step using jsr:
specifiers:
1import { genid } from "jsr:@ndaidong/bellajs"; 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
You can also use npm:
specifiers as before:
1import { genid } from "npm:@ndaidong/bellajs"; 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
Or import from esm.sh
1import { genid } from "https://esm.sh/@ndaidong/bellajs"; 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
Node.js & Bun
https://www.npmjs.com/package/@ndaidong/bellajs
1npm i @ndaidong/bellajs 2# pnpm 3pnpm i @ndaidong/bellajs 4# yarn 5yarn add @ndaidong/bellajs 6# bun 7bun add @ndaidong/bellajs
1import { genid } from "@ndaidong/bellajs"; 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
You can also use CJS style:
1const { genid } = require("@ndaidong/bellajs"); 2 3for (let i = 0; i < 5; i++) { 4 console.log(genid()); 5}
Browsers:
1<script type="module"> 2 import { genid } from "https://esm.sh/@ndaidong/bellajs"; 3 // import { genid } from 'https://unpkg.com/@ndaidong/bellajs/esm/mod.js'; 4 5 for (let i = 0; i < 5; i++) { 6 console.log(genid()); 7 } 8</script>
APIs
DataType detection
.isArray(Anything val)
.isBoolean(Anything val)
.isDate(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.
1import { clone } from "@ndaidong/bellajs"; 2 3const b = [ 4 1, 5 5, 6 0, 7 "a", 8 -10, 9 "-10", 10 "", 11 { 12 a: 1, 13 b: "Awesome", 14 }, 15]; 16 17const cb = clone(b); 18console.log(cb);
cb now has the same values as b, while the properties are standalone, not reference. So that:
1cb[7].a = 2; 2cb[7].b = "Noop"; 3 4console.log(b[7]);
What you get is still:
1{ 2 a: 1, 3 b: 'Awesome' 4}
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.
1import { copies } from "@ndaidong/bellajs"; 2 3const a = { 4 name: "Toto", 5 age: 30, 6 level: 8, 7 nationality: { 8 name: "America", 9 }, 10}; 11const b = { 12 level: 4, 13 IQ: 140, 14 epouse: { 15 name: "Alice", 16 age: 27, 17 }, 18 nationality: { 19 long: "18123.123123.12312", 20 lat: "98984771.134231.1234", 21 }, 22}; 23 24copies(a, b); 25console.log(b);
Output:
1{ 2 level: 8, 3 IQ: 140, 4 epouse: { 5 name: 'Alice', 6 age: 27 7 }, 8 nationality: { 9 long: '18123.123123.12312', 10 lat: '98984771.134231.1234', 11 name: 'America' 12 }, 13 name: 'Toto', 14 age: 30 15}
Array utils
pick(Array arr [, Number count = 1])
Randomly choose N elements from array.
1import { pick } from "@ndaidong/bellajs"; 2 3const arr = [1, 3, 8, 2, 5, 7]; 4pick(arr, 2); // --> [3, 5] 5pick(arr, 2); // --> [8, 1] 6pick(arr); // --> [3] 7pick(arr); // --> [7]
sort(Array arr [, Function compare])
Sort the array using a function.
1import { sort } from "@ndaidong/bellajs"; 2 3const fn = (a, b) => { 4 return a < b ? 1 : a > b ? -1 : 0; 5}; 6 7sort([3, 1, 5, 2], fn); // => [ 1, 2, 3, 5 ]
sortBy(Array arr, Number order, String property)
Sort the array by specific property and direction.
1import { sortBy } from "@ndaidong/bellajs"; 2 3const players = [ 4 { 5 name: "Jerome Nash", 6 age: 24, 7 }, 8 { 9 name: "Jackson Valdez", 10 age: 21, 11 }, 12 { 13 name: "Benjamin Cole", 14 age: 23, 15 }, 16 { 17 name: "Manuel Delgado", 18 age: 33, 19 }, 20 { 21 name: "Caleb McKinney", 22 age: 28, 23 }, 24]; 25 26const result = sortBy(players, -1, "age"); 27console.log(result);
shuffle(Array arr)
Shuffle the positions of elements in an array.
1import { shuffle } from "@ndaidong/bellajs"; 2 3shuffle([1, 3, 8, 2, 5, 7]);
unique(Array arr)
Remove all duplicate elements from an array.
1import { unique } from "@ndaidong/bellajs"; 2 3unique([1, 2, 3, 2, 3, 1, 5]); // => [ 1, 2, 3, 5 ]
Functional utils
curry(fn)
Make a curried function.
1import { curry } from "@ndaidong/bellajs"; 2 3const sum = curry((a, b, c) => { 4 return a + b + c; 5}); 6 7sum(3)(2)(1); // => 6 8sum(1)(2)(3); // => 6 9sum(1, 2)(3); // => 6 10sum(1)(2, 3); // => 6 11sum(1, 2, 3); // => 6
compose(f1, f2, ...fN)
Performs right-to-left function composition.
1import { compose } from "@ndaidong/bellajs"; 2 3const f1 = (name) => { 4 return `f1 ${name}`; 5}; 6const f2 = (name) => { 7 return `f2 ${name}`; 8}; 9const f3 = (name) => { 10 return `f3 ${name}`; 11}; 12 13const addF = compose(f1, f2, f3); 14 15addF("Hello"); // => 'f1 f2 f3 Hello' 16 17const add1 = (num) => { 18 return num + 1; 19}; 20 21const mult2 = (num) => { 22 return num * 2; 23}; 24 25const add1AndMult2 = compose(add1, mult2); 26add1AndMult2(3); // => 7 27// because multiple to 2 first, then add 1 late => 3 * 2 + 1
pipe(f1, f2, ...fN)
Performs left-to-right function composition.
1import { pipe } from "@ndaidong/bellajs"; 2 3const f1 = (name) => { 4 return `f1 ${name}`; 5}; 6const f2 = (name) => { 7 return `f2 ${name}`; 8}; 9const f3 = (name) => { 10 return `f3 ${name}`; 11}; 12 13const addF = pipe(f1, f2, f3); 14 15addF("Hello"); // => 'f3 f2 f1 Hello' 16 17const add1 = (num) => { 18 return num + 1; 19}; 20 21const mult2 = (num) => { 22 return num * 2; 23}; 24 25const add1AndMult2 = pipe(add1, mult2); 26add1AndMult2(3); // => 8 27// because add 1 first, then multiple to 2 late => (3 + 1) * 2
Date utils
formatDateString(Date | Timestamp [, String locale [, Object options]])
1import { formatDateString } from "@ndaidong/bellajs"; 2 3const today = new Date(); 4 5formatDateString(today); // => Jan 3, 2022, 8:34:28 PM GMT+7 6 7// custom format 8formatDateString(today, { 9 dateStyle: "short", 10 timeStyle: "short", 11 hour12: true, 12}); // => 1/3/22, 8:34 PM 13 14// custom locale 15formatDateString(today, "zh"); // => 2022年1月3日 GMT+7 下午8:34:28 16 17// custom lang and format 18formatDateString(today, "zh", { 19 dateStyle: "short", 20 timeStyle: "long", 21 hour12: true, 22}); // => 2022/1/3 GMT+7 下午8:34:28 23 24formatDateString(today, "vi"); // => 20:34:28 GMT+7, 3 thg 1, 2022 25formatDateString(today, "vi", { 26 dateStyle: "full", 27 timeStyle: "full", 28}); // => 20:34:28 Giờ Đông Dương Thứ Hai, 3 tháng 1, 2022
formatTimeAgo(Date | Timestamp [, String locale [, String justnow]])
1import { formatTimeAgo } from "@ndaidong/bellajs";
2
3const today = new Date();
4
5const yesterday = today.setDate(today.getDate() - 1);
6formatTimeAgo(yesterday); // => 1 day ago
7
8const current = new Date();
9const aLittleWhile = current.setHours(current.getHours() - 3);
10formatTimeAgo(aLittleWhile); // => 3 hours ago
11
12// change locale
13formatTimeAgo(aLittleWhile, "zh"); // => 3小时前
14formatTimeAgo(aLittleWhile, "vi"); // => 3 giờ trước
The last param justnow
can be used to display a custom 'just now' message,
when the distance is lesser than 1s.
1const now = new Date();
2const aJiff = now.setTime(now.getTime() - 100);
3formatTimeAgo(aJiff); // => 'just now'
4formatTimeAgo(aJiff, "fr", "à l'instant"); // => à l'instant
5formatTimeAgo(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
1import { randint } from "@ndaidong/bellajs"; 2 3randint(); // => a random integer 4randint(1, 5); // => a random integer between 3 and 5, including 1 and 5
genid([Number length [, String prefix]])
Create random ID string.
1import { genid } from "@ndaidong/bellajs"; 2 3genid(); // => random 32 chars 4genid(16); // => random 16 chars 5genid(5); // => random 5 chars 6genid(5, "X_"); // => X_{random 3 chars}
Development
Since v12.x.x, we switched to Deno platform, and use DNT to build Node.js packages.
1git clone https://github.com/ndaidong/bellajs.git 2cd bellajs 3 4# test 5deno test 6 7# build npm packages 8deno task build 9 10cd npm 11node test_runner.js
License
The MIT License (MIT)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 9 commits out of 30 are checked with a SAST tool
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 0/11 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci-test.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/ci-test.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci-test.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/ci-test.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci-test.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/ci-test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:45: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/ndaidong/bellajs/publish.yml/main?enable=pin
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:17
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:18
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/publish.yml:12
- Warn: no topLevel permission defined: .github/workflows/ci-test.yml:1
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4.1
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More