![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Lightweight util for handling data type, string, data entries, datetime in your Node.js and browser apps.
Node.js
npm install bellajs --save
CDN
This library also supports ES6 Module, AMD and UMD style.
This library provides a function named "stabilize" that you can pass through it an object or array to get back immutable version.
Here is an example in Node.js:
var stabilize = require('bellajs').stabilize;
let user = stabilize({
name: 'Bob',
age: 17
});
// user now is immutable
console.log(user);
// access the properties with get() or dot
let name = user.get('name'); // similar to user.name
console.log(name);
// you can change properties' value with set() method
// it will return a copy of user with new property
let guest = user.set('name', 'Tom');
console.log(guest.name); // => Tom
// the value of user.name can not be changed
console.log(user.name); // => Bob
// because it's immutable
user.name = 'Jerry';
console.log(user.name); // => Bob
stabilize(Object o);
Return an immutable object that has 2 methods "set()" and "get()".
Because the returned object is standard object, so you can still use the built-in methods in Object.prototype as normal.
Return value of specified property.
Return an new immutable object with new property.
Setter also accepts an object to allow to define many properties at the same time:
let car = stabilize({
name: '',
speed: 1000,
color: 'black'
});
let tesla = car.set({
name: 'Tesla',
price: 40000
});
console.log(tesla);
tesla now is a new object with the following properties:
{
name: 'Tesla',
speed: 1000,
color: 'black',
price: 40000
}
stabilize(Array a);
Return an immutable array that has the following methods:
Because the returned array is standard array, so you can still use the built-in methods in Array.prototype as normal.
Return a new array with no duplicate elements.
let a = stabilize([1, 2, 2, 3, 2, 4]);
let b = a.unique();
console.log(b); // => [ 1, 2, 3, 4 ]
Return the smallest value from an array of numbers.
let a = stabilize([1, 2, 2, 3, 8, 5, 2, 4]);
let b = a.min();
console.log(b); // => 1
Return the biggest value from an array of numbers.
let a = stabilize([1, 2, 2, 3, 8, 5, 2, 4]);
let b = a.max();
console.log(b); // => 1
Return the first element from array.
let a = stabilize([1, 2, 2, 3, 2, 4]);
let b = a.first();
console.log(b); // => 1
Return the last element from array.
let a = stabilize([1, 2, 2, 3, 2, 4]);
let b = a.last();
console.log(b); // => 4
Extract count elements from array in randomly order.
let a = stabilize([1, 2, 2, 3, 2, 4]);
let b = a.pick(3);
console.log(b); // output an array of 3 random elements
Without count, this method returns a random element.
Return a new array with new elements inserted at the position specified by first parameter.
let a = stabilize([1, 2, 3, 4]);
let b = a.insert(2, 'a');
console.log(b); // => [ 1, 2, 'a', 3, 4 ]
Return a new array with new elements added at the end.
let a = stabilize([1, 2, 3, 4]);
let b = a.append(5, 6, 7);
console.log(b); // => [ 1, 2, 3, 4, 5, 6, 7 ]
Return a new array with count elements deleted beginning at start:
let a = stabilize([1, 2, 3, 4, 5, 6]);
let b = a.remove(3, 2); // remove 2 items from index 3, means 4 and 5
console.log(b); // => [ 1, 2, 3, 6 ]
Return a new array sorted by compareFunction.
This method does the same thing as Array.sort, but immutable.
let users = stabilize([
{
name: "Bob",
age: 28
},
{
name: "Anne",
age: 21
},
{
name: "Jim",
age: 33
},
{
name: "Kate",
age: 17
}
]);
let sortedUsers = users.isort((a, b) => {
let ag = a.age;
let bg = b.age;
if (ag === bg) {
return 0;
}
return ag < bg ? -1 : 1;
});
console.log(sortedUsers);
Output:
[ { name: 'Kate', age: 17 },
{ name: 'Anne', age: 21 },
{ name: 'Bob', age: 28 },
{ name: 'Jim', age: 33 } ]
Advanced version of .isort() that allows to sort an array by some flexible ways.
var points = stabilize([1, 5, 19, 6, 4, 11, 7, 22, 40, 3, 8]);
console.log('Array points, original:');
console.log(points);
console.log('Array points, lowest to highest:');
var a1 = points.msort(); // without parameter
console.log(a1);
console.log('Array points, descendant:');
var a2 = points.msort(-1);
console.log(a2);
var players = stabilize([
{
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
}
]);
console.log('\nList of players as it is:');
players.forEach((item) => {
console.log([item.name, item.age].join(' | '));
});
console.log('\nSort by age from youngest to oldest:');
var players1 = players.msort('age');
players1.forEach((item) => {
console.log([item.name, item.age].join(' | '));
});
console.log('\nSort by age from oldest to youngest:');
var players2 = players.msort({age: -1});
players2.forEach((item) => {
console.log([item.name, item.age].join(' | '));
});
Results:
This method does the same thing as Array.reverse, but immutable.
For example now you can reverse the above sortedUsers array:
let reversedUsers = sortedUsers.ireverse();
console.log(reversedUsers);
Output:
[ { name: 'Jim', age: 33 },
{ name: 'Bob', age: 28 },
{ name: 'Anne', age: 21 },
{ name: 'Kate', age: 17 } ]
Return a clone of given array with shuffled elements.
let shuffledUsers = sortedUsers.shuffle();
console.log(shuffledUsers);
Returns an object with .compile() method
Example:
var tpl = [
'<article>',
'<a href="{link}">{title}</a>',
'<p>{content}</p>',
'<p>',
'<span>{author.name}</span>',
'<span>{author.email}</span>',
'</p>',
'</article>'
].join('');
var data = {
title: 'Hello world',
link: 'http://google.com',
content: 'This is an interesting thing, is that right?',
author: {
name: 'Dong Nguyen',
email: 'ndaidong@gmail.com'
}
}
var html = Bella.template(tpl).compile(data);
console.log(html);
Example:
let d = bella.date(1479374694886);
d.local(); //=> Thu, 17 Nov 2016 16:24:54 GMT+0007
d.utc(); //=> Thu, 17 Nov 2016 09:24:54 GMT
d.relativize(); //=> 2 minutes ago
d.format('Y/m/d h:i:s'); //=> 2016/02/18 15:28:20
Default BellaJS DateTime pattern is 'D, M d, Y H:i:s A'.
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
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'
}
Copy the properties from source to target.
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
}
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
The npm package bellajs receives a total of 12,031 weekly downloads. As such, bellajs popularity was classified as popular.
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.