Skip to content

Commit

Permalink
feat: Add .numbers() to get contained numbers and .subranges() to get…
Browse files Browse the repository at this point in the history
… subranges (#2)

* docs(readme): fix format problem in readme

* types: add TypeScript definitions

* types: Add types/index.d.ts to package.json

* feat: add .numbers() and .subranges()

* fix: fix typo
  • Loading branch information
Zhu Zijia authored and fent committed Sep 15, 2018
1 parent 018a941 commit dc2e624
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 4 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,37 @@ Intersect all of another DRange's subranges
Get the number at the specified index

```js
var drange = DRange()
var drange = new DRange()
drange.add(1, 10);
drange.add(21, 30);
console.log(drange.index(15)); // 25
```

### DRange#numbers()
Get contained numbers

```js
var drange = new DRange(1, 4)
drange.add(6);
drange.subtract(2);
console.log(drange.numbers()); // [1, 3, 4, 6]
```

### DRange#subranges()
Get copy of subranges

```js
var drange = new DRange(1, 4)
drange.add(6, 8);
console.log(drange.subranges());
/*
[
{ low: 1, high: 4, length: 4 },
{ low: 6, high: 8, length: 3 }
]
*/
```

### DRange#clone()
Clones the drange, so that changes to it are not reflected on its clone

Expand Down
19 changes: 19 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ class DRange {
clone() {
return new DRange(this);
}

numbers() {
return this.ranges.reduce((result, subrange) => {
var i = subrange.low;
while (i <= subrange.high) {
result.push(i);
i++;
}
return result;
}, []);
}

subranges() {
return this.ranges.map((subrange) => ({
low: subrange.low,
high: subrange.high,
length: 1 + subrange.high - subrange.low
}));
}
}

module.exports = DRange;
35 changes: 35 additions & 0 deletions test/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,38 @@ describe('clone sets', () => {
assert.equal('[ 0-4, 6-9 ]', erange.toString());
});
});

describe('accessing numbers', () => {
it('should be able to get contained numbers', () => {
var drange = new DRange(1, 4);
drange.subtract(2);
drange.add(6);
var numbers = drange.numbers();
assert.deepStrictEqual([1, 3, 4, 6], numbers);
drange.subtract(3);
assert.deepStrictEqual([1, 3, 4, 6], numbers);
});
});

describe('accessing subranges', () => {
it('should be able to get copy of subranges', () => {
var drange = new DRange(1, 4);
drange.add(6, 8);
var subranges = drange.subranges();
var expect = [
{
low: 1,
high: 4,
length: 4
},
{
low: 6,
high: 8,
length: 3
}
];
assert.deepStrictEqual(expect, subranges);
drange.subtract(6, 8);
assert.deepStrictEqual(expect, subranges);
});
})
6 changes: 6 additions & 0 deletions types/drange-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as DRange from "drange";

type SubRange = DRange.SubRange;

new DRange(); // $ExpectType DRange
new DRange(1); // $ExpectType DRange
new DRange(3, 5); // $ExpectType DRange
Expand All @@ -26,3 +28,7 @@ drange.index(2); // $ExpectType number
drange.clone(); // $ExpectType DRange

drange.toString(); // $ExpectType string

drange.numbers(); // $ExpectType number[]

drange.subranges(); // $ExpectType SubRange[]
18 changes: 17 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
declare namespace DRange {}
declare namespace DRange {
interface SubRange {
low: number;
high: number;
length: number;
}
}

/**
* For adding/subtracting sets of range of numbers.
Expand Down Expand Up @@ -52,6 +58,16 @@ declare class DRange {
clone(): this;

toString(): string;

/**
* Get contained numbers
*/
numbers(): number[];

/**
* Get copy of subranges
*/
subranges(): DRange.SubRange[];
}

export = DRange;
1 change: 0 additions & 1 deletion types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
}
}
}

2 changes: 1 addition & 1 deletion types/tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-implicit-dependencies": false
"file-name-casing": false
}
}

0 comments on commit dc2e624

Please sign in to comment.