npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

booleanjs

v1.0.1

Published

A boolean caculation tool for Object or Array. 对象或数组进行布尔运算的工具

Downloads

4

Readme

booleanjs

booleanjs是一个为Object和Array进行布尔运算(交/并/补)的轻量级工具库。

使用说明

目前兼容commonjs AMD 以及普通的加载方式

commonjs/AMD

使用npm进行安装

npm install booleanjs
var bool = require('booleanjs');

普通加载

使用script标签引入

<script src="path/to/booleanjs/dist/boolean.min.js"></script>
var bool = window.bool;

API

下面是bool的方法列表:

same

{Object|Array} same({Object|Array}obj, ...{Object|Array|null}objs)

返回多个Object或多个Array的交集

注: 第一个参数必须是{Object}或者是{Array},否则会报错

当第一个参数为{Object}时,objs可传入的值为:

  • {Array} 要保留的属性keys数组,如:['a', 'b', 'c'],会将不在该数组里的属性移除;
  • {Object} 普通对象,只有属性name和value都分别相等时,该属性才会保留下来;
  • {null|undefined} 空参数,直接将该参数忽略

Example

// a = {b: 2};
var a = bool.same(
    {a: 1, b: 2, c: 3},
    {a: 1, b: 2},
    {b: 2, c: 4}
);

// a = {b: 2};
var a = bool.same(
    {a: 1, b: 2, c: 3},
    ['b', 'c'],
    {a: 1, b: 2}
);

当第一个参数为{Array}时,objs可传入的值为:

  • {Array} 普通数组,则返回与该数组拥有共同元素的数组,元素顺序与obj中的顺序保持一致;
  • {null|undefined} 空参数,直接将该参数忽略

Example

// a = ['1', 2];
var a = bool.same(
    ['1', 2, '3'],
    [2, 4, '1']
);

// a = [];
var a = bool.same(
    ['1', 2, '3'],
    [2, '1'],
    [1]
);

merge

{Object|Array} merge({Object|Array}obj, ...{Object|Array|null}objs)

返回多个Object或多个Array的并集

当第一个参数为{Object}时,效果跟Object.assign类似,区别在于,当属性的value为{Object}或{Array}时,不会对该属性做拷贝。

objs能传入的值为:

  • {Object} 普通对象
  • {null|undefined} 忽略该参数

Example

var a = {b: 1};
var x = {d: 1, e: a};

// c = {d: 2, e: {b: 1}, f: 3}
// c === x
var c = bool.merge(
    x,
    {d: 2},
    {f: 3}
);

// c = {d: 2, e: {b: 2}, f: 3}
a.b = 2;

当第一个参数为{Array}时,objs可传入的值为:

  • {Array} 普通数组,会首先将该数组中与obj重复的的元素过滤,再将剩余的元素append到obj中,同时返回obj数组;
  • {null|undefined} 忽略该参数

Example

var a = [1, 2, 3, 4];

// b = [1, 2, 3, 4, 7, 6]
// a === b
var b = bool.merge(
    a,
    [7, 4, 1],
    [2, 6]
);

exclude

{Object|Array} exclude({Object|Array}obj, ...{Object|Array}objs)

返回obj与objs的补集

当第一个参数为{Object}时,objs可传入的值为:

  • {Array} 要保留的属性keys数组,如:['a', 'b', 'c'],会将在该数组里的属性移除;
  • {Object} 普通对象,只有属性name和value都分别相等时,该属性才会被移除;
  • {null|undefined} 空参数,直接将该参数忽略

Example

// a = {d: 4}
var a = bool.exclude(
    {a: 1, b: 3, c:3, d: 4},
    {c: 4, d: 4},
    ['a', 'b']
);

当第一个参数为{Array}时,objs可传入的值为:

  • {Array} 普通数组,则排除与该数组拥有共同元素的数组,元素顺序与obj中的顺序保持一致;
  • {null|undefined} 空参数,直接将该参数忽略

Example

// a = [2]
var a = bool.exclude(
    [1, 2, 3, 4],
    [3, 1, 7],
    [1, 4, 6]
);

isEqual

{boolean} isEqual({*}obj1, {*}obj2)

判断两个对象是否深度相等,应该跟underscore实现的差不多

** 注 ** 能判断的类型有

  • string
  • number
  • null
  • undefined
  • Array
  • Object
  • Date
  • RegExp

Example

// true
var a = bool.isEqual({}, {});

// false
var a = bool.isEqual(-0, +0);

// true
var a = bool.isEqual(NaN, NaN);

isContain

{boolean} isContain({*}obj1, {*}obj2)

判断obj1是否包含obj2

// true
var a = bool.isContain({}, null);

// true
var a = bool.isContain({a: 1, b: {c: 2}}, {b: {c: 2}});

// true
var a = bool.isContain([1, 2, 3, 4], [4, 3]);