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

int-range

v1.6.0

Published

int-range 是用来计算范围的类库。

Downloads

18

Readme

int-range

int-range 是用来计算范围的类库。

Install:

npm i int-range

Demo:

创建范围:

const {Range, range} = require("int-range");

// 方式1
let r1 = new Range(2, 3); // 创建 begin=2,length=3 的范围
r1.toRaw();               // [2,3,4]
let r2 = new Range(0, 1); // 创建 begin=0,length=1 的范围
r2.toRaw();               // [0]

// 方式2
let r3 = range(2, 4);     // 创建 min=2,max=4 的范围
r3.toRaw();               // [2,3,4]
let r4 = range(0, 0);     // 创建 min=0,max=0 的范围
r4.toRaw();               // [0]

范围运算:

目前共支持4种运算函数:rangeAdd(并集)、rangeSub(差集)、rangeIntersect(交集)、rangeCut(切割)。这些函数接受的参数类型是一个或多个 Range 对象,返回的运算结果是 Array

const {range, rangeAdd, rangeSub, rangeIntersect, rangeCut} = require("int-range");

// 并集
let r1 = range(1,3);                // [1,2,3]
let r2 = range(2,4);                // [2,3,4]
let r3 = range(9,10);               // [9,10]
let result = rangeAdd(r1, r2, r3);  // 计算 r1、r2 的并集
result instanceof Array;            // true
result.length;                      // 2
result;                             // [[1,2,3,4],[9,10]]

// 差集
r1 = range(1,6);                    // [1,2,3,4,5,6]
r2 = range(3,4);                    // [3,4]
rangeSub(r1, r2);                   // [[1,2],[5,6]]

// 交集
r1 = range(1,3);                    // [1,2,3]
r2 = range(2,4);                    // [2,3,4]
rangeIntersect(r1, r2);             // [[2,3]]

// 切割
// 可以理解为:(r1 - r2) ∪ (r1 ∩ r2) ∪ (r2 - r1)
r1 = range(1,4);                    // [1,2,3,4]
r2 = range(3,6);                    // [3,4,5,6]
rangeCut(r1, r2);                   // [[1,2],[3,4],[5,6]]

使用 RangeGroup

虽然可以使用以上函数方便的进行运算,但是有一个缺点:运算结果是数组类型,无法进行下一次运算。所以,当有连续运算的需求时,应该使用 RangeGroup 对象。RangeGroup 实现了以上四种运算方法,并且支持链式调用。

const {range, RangeGroup} = require("int-range");

let rg = new RangeGroup();          // []
rg.add(range(1,4)).add(range(3,6)); // [[1,2,3,4,5,6]] (使用链式调用)
rg.intersect(range(3,4));           // [[1,2],[5,6]]
rg.sub(range(5,5));                 // [[1,2],[6]]
rg.cut(range(1,6));                 // [[1,2],[3,4],[5],[6]]

Update:

| 版本 | 更新内容 | | ------ | --- | | 1.6.0 | 1、使用 webpack 打包2、新增方法:RangeGroup.reset() | | 1.5.0 | 新增方法:RangeGroup.equal()RangeGroup.isIntersect() | | 1.4.0 | 1、新增 RangeGroup2、修复 Range.isIntersect(undefine) 引起报错的 BUG | | 1.3.3 | 1、修复 Range.begin 赋值异常的 BUG2、优化项目结构,增加单元测试 | | 1.3.2 | 修复 rangeSub(range(0)) 结果异常的 BUG | | 1.3.1 | 优化 range() 方法,可以返回 length=0IntRange 实例例如: range(2) 等价于 new IntRange(2,0)| | 1.3.0 | 1、支持 IntRange.length = 02、新增方法:IntRange.toRaw()3、优化算法| | 1.2.0 | 新增方法:[静态] IntRange.create()[静态] IntRange.release()IntRange.equal()|