Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bellajs

Package Overview
Dependencies
Maintainers
1
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bellajs

A useful helper for any javascript program

  • 7.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
increased by10.43%
Maintainers
1
Weekly downloads
 
Created
Source

BellaJS

Lightweight util for handling data type, string... in your Node.js and browser apps.

NPM Build Status codecov Dependency Status NSP Status

Contents

Setup

  • Node.js

    npm install bellajs
    
  • CDN

  • Also supports ES6 Module, CommonJS, AMD and UMD style.

Usage

var bella = require('bellajs');

// or:
import bella from 'bellajs';

// or import several methods only
import {
  isArray,
  isString
} from 'bellajs';

// similar:
var {
  isArray,
  isString
} = require('bellajs');

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)
  • .isNull(Anything val)
  • .isNumber(Anything val)
  • .isObject(Anything val)
  • .isString(Anything val)
  • .isUndefined(Anything val)

String manipulation

  • .createAlias(String s)
  • .encode(String s)
  • .decode(String s)
  • .ucfirst(String s)
  • .ucwords(String s)
  • .escapeHTML(String s)
  • .unescapeHTML(String s)
  • .stripTags(String s)
  • .stripAccent(String s)
  • .trim(String s [, Boolean nospace])
  • .truncate(String s, Number limit)
  • .repeat(String s, Number times)
  • .leftPad(String s, Number limit, String pad)
  • .rightPad(String s, Number limit, String pad)
  • .replaceAll(String s, String|Array search, String|Array replace)

Template

  • .template(String tpl)

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);

Other utils

.clone(Anything val):

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'
}
.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.

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
}
.createId([Number length [, String prefix]])
import {createId} from 'bellajs';

createId(); // => random 32 chars
createId(16); // => random 16 chars
createId(5); // => random 5 chars
createId(5, 'X_'); // => X_{random 3 chars}
.equals(Anything a, Anything b)
import {equals} from 'bellajs';

equals({}, {}); // => true
equals(0, 1); // => false
.md5(String s)
import {md5} from 'bellajs';

md5('abc'); // => 900150983cd24fb0d6963f7d28e17f72
.random([Number min [, Number max]])
import {random} from 'bellajs';

random(); // => a random integer
random(1, 5); // => a random integer between 3 and 5, including 1 and 5
.unique(Array a)
import {unique} from 'bellajs';

unique([1, 2, 3, 2, 3, 1, 5]); // => [ 1, 2, 3, 5 ]
.curry(fn)
import {curry} from 'bellajs';

let 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
.compose(f1, f2, ...fN)

Performs right-to-left function composition.

import {compose} from 'bellajs';

let f1 = (name) => {
  return `f1 ${name}`;
};
let f2 = (name) => {
  return `f2 ${name}`;
};
let f3 = (name) => {
  return `f3 ${name}`;
};

let addF = compose(f1, f2, f3);

addF('Hello') // => 'f1 f2 f3 Hello'

let add1 = (num) => {
  return num + 1;
};

let mult2 = (num) => {
  return num * 2;
};

let add1AndMult2 = compose(add1, mult2);
add1AndMult2(3) // => 7
// because multiple to 2 first, then add 1 late => 3 * 2 + 1
.pipe(f1, f2, ...fN)

Performs left-to-right function composition.

import {pipe} from 'bellajs';

let f1 = (name) => {
  return `f1 ${name}`;
};
let f2 = (name) => {
  return `f2 ${name}`;
};
let f3 = (name) => {
  return `f3 ${name}`;
};

let addF = pipe(f1, f2, f3);

addF('Hello') // => 'f3 f2 f1 Hello'

let add1 = (num) => {
  return num + 1;
};

let mult2 = (num) => {
  return num * 2;
};

let add1AndMult2 = pipe(add1, mult2);
add1AndMult2(3) // => 8
// because add 1 first, then multiple to 2 late => (3 + 1) * 2

Note

Some parts of bella have been split to separate modules, including:

Test

git clone https://github.com/ndaidong/bellajs.git
cd bellajs
npm install
npm test

License

The MIT License (MIT)

Keywords

FAQs

Package last updated on 14 Jun 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc