Skip to content

Latest commit

 

History

History
executable file
·
488 lines (409 loc) · 24.5 KB

README_ZH.md

File metadata and controls

executable file
·
488 lines (409 loc) · 24.5 KB

中文 / English

logo


npm version gzip size monthly npm installs jest codecov-image license

bbo 项目是一个实用的 javba 函数工具库。

使用 node,react,vue,angular,webpack 等进行项目开发时,需要编写许多 utils 方法,并且开发中还需要不断重写很多函数,使用 bbo 可以简单经凑地解决这些问题。

概述

每个前端开发人员都有自己的 utils 库, 这些方法我们高频使用,但又要在每个项目中重写。 bbo 是一款超小且实用的函数工具库,而且不同于 lodash underscore lazy.js.

项目整理了日常开发中最常用的功能。 这些功能在您的开发中几乎无处不在,并且在主流的函数工具库中找不到。大多数代码来自于高分答案中的stackoverflow.com 网站,向原始作者表示敬意。项目在 gzip 压缩下只有 7K, 所以你可以随时随地使用。

请参阅最新的文档 Documentation 以获取完整的 API 参考,或者在 github 上贡献bbo-docs文档。

文档

功能

device args http string array
ua args open trim unique
isIos noop getUrlParam fillZero uniqueBy
isiPhone merge setUrlParam longUnique uniqueFrom
isIPad over deleteUrlParam stripTags random
isAndroid call objectParam capitalize randomSize
isMobile hasOwnProperty httpGet deCapitalize shuffle
isPC bom httpPost isAbsoluteURL contains
isWeixin stopPropagation random mapString includesAll
isNewsApp g randomColor mask includesAny
isQQ gc randomA2B splitLines removeAt
isQQbrowser c randomKey camelize remove
isTenvideo query behavior underscored compact
isWeiShi show trigger dasherize pluck
isIphoneXmodel hide lockTouch truncate union
isIE elementContains copyToClipboard byteSize unionBy
ieVersion formToObject mlodash byteLen unionWith
log getStyle getTag repeat intersect
log setStyle is endsWith intersectBy
logs attr isObject startsWith difference
removeConsole load isArray containsWith differenceBy
trash loadImages isString xssFilter max
other loadjs isBoolean effortIndex min
uuid loadcss isNumber capwords equal
hash fill isMap object allEqual
judge fill0 isSet properObject all
getType floor isFunction objectDiff any
isTypeof chainAsync isEmpty addedDiff chunk
construct numberFormat isShallowEqual deletedDiff countBy
paramsName modulo has detailedDiff countOccurrences
eventEmitter cookie toPath updatedDiff drop
times cookie reduce collection dropRight
setTimesout setCookie forEach clone dropWhile
clearTimesout getCookie map entries dropRightWhile
getDate deleteCookie find extend column
formatPassTime parseCookie findIndex flush split
formatRemainTime image get values unary
formatDuration checkImageSize set size indexBy
sleep imageOptimization debounce search
retry toDataUrl throttle
json pick
toJson omit
jsonp isSymbol
storage isDate
storage mapValues

使用

范例

// 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]

... 

引用

bbo

可以在 Node.js, Rollup, Webpack, Browserify 等环境中使用。

commonjs

使用 npm 安装

npm install bbo --save

CommonJS

使用整个库

const bbo = require('bbo');
bbo.isiPhone(); // => 'true'

个别功能:

const cookie = require('bbo/cookie');

ES2015

import bbo from 'bbo';

导入单个功能:

import storage from 'bbo/storage';

浏览器

browsers

直接将 js 引入到浏览器中

<script src="bbo.min.js" type="text/javascript"></script>

使用bbo为全局变量

<script type="text/javascript">
  bbo.cookie().getJson(); // => {a: 1, b: 2}
</script>

CDN

https://mat1.gtimg.com/www/js/libs/bbo.min.js
// 国内用户可以直接使用此cdn

开发

依赖 nodejs, 请使用 terminal/iTerm 安装环境。

构建项目

git clone https://github.com/tnfe/bbo.git

...
npm install
npm run lint
npm run build

运行项目

npm run start
// 访问 http://localhost:8080

贡献内容

如果你想参与这个项目的共同创作,修改或添加内容,可以先 Fork 这个项目的仓库,然后将修改的内容提交 Pull requests ;或者创建 Issues

Fork 后的仓库如何同步本仓库?

# 添加 upstream 源,只需执行一次
git remote add upstream git@github.com:tnfe/bbo.git

# 拉取远程代码
git pull upstream master

# 提交修改
git add .
git commit

# 更新 fork 仓库
git push origin master

更多参考: Syncing a fork

贡献文档

文档使用 Vuepress 撰写并生成网站,请查看文档仓库 package.json 中的 scripts 配置和 /docs 目录中的脚本来了解文档的构建和发布过程。

# 初始化 nodejs 依赖
npm install

# 安装 vuepress 插件
npm install -g vuepress

# 进入图书目录
cd docs

# 开始写作
vuepress dev .

# 构建静态文件
vuepress build .

# 查看本地文档内容
# 访问 http://localhost:8080

维护者

@halldwang.

贡献者

感谢为 bbo 做出贡献。

https://github.com/tnfe/bbo/graphs/contributors

Changelog

Detailed changes for each release are documented in the release notes.

License

MIT.