Awesome
<p align="right"><a href="./README_ZH.md">中文</a> / <a href="./README.md">English</a></p>bbo is a utility library of zero dependencies for javascript.
Overview
Every frontend developer has his own utils library, and we often write methods that are easily forgotten and highly used. bbo is a super small and useful utils library for JavaScript. It isn't couping with lodash underscore lazy.js.
I sorted out the most frequently used functions in daily development. These functions are almost ubiquitous in your development, and they cannot be found in lodash and underscore.
Most code comes from the stackOverflow site in the high-score answers, so we pay tribute to the original authors.
With easy code and less than 7k gzip, bbo can be used anytime and anywhere with no worries.
See the latest docs/documentation for a full API reference or bbo-docs.
Why
When you use react, vue, angular,you often need to write a lot of utils methods. But lodash and underscore libraries are not omnipotent. So you have to find a lot of tool libraries. By using bbo, you can solve many small problems in the daily development. It is simple and compact!
Documentation
Functions
Usage
example
// base case
bbo.getCookie('username'); // => 'userName'
bbo.cookie().getJson(); // => {a: 1, b: 2}
bbo.isiPhone(); // => true or false
bbo.numberFormat(1234.56, 2, ',', ' '); // => '1 234,56';
bbo.split([1, 2, 3, 4, 5], 2); // => [[1,2], [3,4], [5]]
bbo.entries({ c: 8, a: 4 }); // => [['c', 8], ['a', 4]]
bbo.toPath("a.b.c"); // => ['a', 'b', 'c']
bbo.get({ a: { aa: { aaa: 2 } }, b: 4 }, "a.aa.aaa"); // => 2
bbo.union([1, 2, 3], [4, 3, 2]); // => [1, 2, 3, 4]
bbo.intersect([1, 2, 3], [4, 3, 2]); // => [2, 3]
bbo.unionBy([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2]
bbo.mapValues({ a: 3, b: 5, c: 9 }, (value) => value + 1); //=> {a: 4, b: 6, c: 10}
bbo.compact([0, 1, false, 2, "", 3]); // [1, 2, 3]
bbo.flush({a: 2, b: null, c: 4, d: undefined}); // => {a: 2, c: 4}
bbo.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // => [1]
bbo.search("3", { a: 3, b: 5, c: 7 }); // => 'a'
bbo.size({ a: 1, b: 2 }); // => 2
var users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false },
];
bbo.find(users, { age: 1, active: true }); // => {"active": true, "age": 36, "user": "barney"}
bbo.findIndex(users, ["active", false]); // => 1
// chain case
var array1 = [1, 2, 3, null];
var array2 = [3, 4, 5, ''];
var object1 = { a: 6, b: 7 };
var object2 = { c: 8, d: 9 };
bbo
.chain(object1)
.extend(object2) // => {a: 6, b: 7, c: 8, d: 9}
.entries() // => [["a", 6], ["b", 7], ["c", 8], ["d", 9]]
.thru((words) => {
const temp = [];
bbo.forEach(words, (item, index) => {
temp.push(item[1]);
});
return temp;
}) // => [6, 7, 8, 9]
.union(array1) // => [6, 7, 8, 9, 1, 2, 3, null]
.union(array2) // => [6, 7, 8, 9, 1, 2, 3, null, 4, 5, ""]
.compact() // => [6, 7, 8, 9, 1, 2, 3, 4, 5]
.thru((array) => {
return array.sort();
}) // => [1, 2, 3, 4, 5, 6, 7, 8, 9]
.value();
// return => [1, 2, 3, 4, 5, 6, 7, 8, 9]
... ∞
Install
bbo supports Node.js, Rollup, Webpack, Browserify。
npm
Install the library with npm into your local modules directory:
npm install bbo --save
CommonJS modules
Then in your application require the entire library:
const bbo = require('bbo');
bbo.isiPhone(); // => 'true'
Or require individual functions:
const cookie = require('bbo/cookie');
ES2015 modules
Bbo is compatible with ES2015 modules to import the entire library:
import bbo from 'bbo';
Or import individual functions:
import storage from 'bbo/storage';
Browser
Load the UMD builds directly into browser's web page:
- dist/bbo.min.js , minified production-ready, with source map
- dist/bbo.js uncompressed with comments
<script src="bbo.min.js" type="text/javascript"></script>
Then a global variable bbo
is exposed for the entire library:
<script type="text/javascript">
bbo.cookie().getJson(); // => {a: 1, b: 2}
</script>
CDN
Building
node is a dependency, use terminal/iTerm to install it with
Clone git repository:
git clone git://github.com/tnfe/bbo.git
Install dependencies:
npm install
Build bundle
npm run build
And run example
npm run start
//visit http://localhost:8080
Testing
- Run all tests as a single test suite with
npm run test
- Show the world you're using Jest.
Coverage sunburst
Each block represents a single file in the project(codecov.io). The size and color of each block is represented by the number of statements and the coverage, respectively.
Contribution
Contribution is welcome! If you wish to contribute to the project, please send the pull requests to the develop branch
-
Create a pull request containing bug fixes or new features. 😎
-
Propose new functions, improvements, better documentation
See contributors.
If you want to participate in the creation of this project,Edit or add function,Fork this project,Modify and Pull requests or new Issues.
How to sync?
# Add upstream origin,Just execute it once
git remote add upstream git@github.com:tnfe/bbo.git
# Pull remote code
git pull upstream master
# Commit changes
git add .
git commit
# update fork
git push origin master
More: Syncing a fork
Changelog
Detailed changes for each release are documented in the release notes.
License
bbo is MIT licensed.