Skip to content

Commit

Permalink
Implemented equal.
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Mar 24, 2021
1 parent c33316f commit fbd7581
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ That is why `ranges-set` operates on actual _ranges_, resulting in performance s
## Usage

```ts
import { difference, expand, intersection, normalize, subset, union } from 'ranges-set';
import { difference, equal, expand, intersection, normalize, subset, union } from 'ranges-set';

difference('1-4', '2-3'); // '1,4'
equal('1-3', '1-2'); // false
expand('1-3,5-7'); // ['1', '2', '3', '5', '6', '7']
intersection('1-10', '5-10'); // '5-10'
normalize('1,2,3,5,6-8'); // '1-3,5-8'
Expand All @@ -31,6 +32,7 @@ union('1-60,40-100'); // '1-100'

```ts
function difference(textA: string, textB: string): string;
function equal(textA: string, textB: string): boolean;
function expand(text: string): string[];
function intersection(textA: string, textB: string): string;
function normalize(text: string): string;
Expand Down
58 changes: 41 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ type ReprRange = { kind: typeof KindRange; min: number; max: number };
const numberPattern = /^\s*(?:0|[1-9]\d*)\s*$/;
const rangePattern = /^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;

function compare(reprA: Repr, reprB: Repr): number {
if (reprA.kind !== reprB.kind) {
return reprA.kind - reprB.kind;
}

switch (reprA.kind) {
case KindLiteral:
return reprA.text >= (reprB as ReprLiteral).text
? reprA.text > (reprB as ReprLiteral).text
? 1
: 0
: -1;
case KindRange:
return (
reprA.min - (reprB as ReprRange).min ||
reprA.max - (reprB as ReprRange).max
);
}
}

export function difference(textA: string, textB: string): string {
const reprsA = parse(textA);
const reprsB = parse(textB);
Expand Down Expand Up @@ -75,6 +95,26 @@ function differenceReprs(reprsA: Repr[], reprsB: Repr[]): Repr[] {
return reprs;
}

export function equal(textA: string, textB: string): boolean {
const reprsA = parse(textA);
const reprsB = parse(textB);
return equalReprs(reprsA, reprsB);
}

function equalReprs(reprsA: Repr[], reprsB: Repr[]): boolean {
if (reprsA.length !== reprsB.length) {
return false;
}

for (let index = 0; index < reprsA.length; ++index) {
if (compare(reprsA[index], reprsB[index]) !== 0) {
return false;
}
}

return true;
}

export function expand(text: string): string[] {
const reprs = parse(text);
return expandReprs(reprs);
Expand Down Expand Up @@ -144,22 +184,6 @@ export function normalize(text: string): string {
return serialize(reprs);
}

function order(reprA: Repr, reprB: Repr): number {
if (reprA.kind !== reprB.kind) {
return reprA.kind - reprB.kind;
}

switch (reprA.kind) {
case KindLiteral:
return reprA.text < (reprB as ReprLiteral).text ? -1 : 1;
case KindRange:
return (
reprA.min - (reprB as ReprRange).min ||
reprA.max - (reprB as ReprRange).max
);
}
}

function parse(text: string): Repr[] {
return unionReprs(text.split(',').filter(Boolean).map(parseOne));
}
Expand Down Expand Up @@ -269,5 +293,5 @@ function unionReprReducer(reprs: Repr[], repr: Repr): Repr[] {
}

function unionReprs(reprs: Repr[]): Repr[] {
return reprs.sort(order).reduce(unionReprReducer, []);
return reprs.sort(compare).reduce(unionReprReducer, []);
}
43 changes: 42 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import test from 'ava';

import { difference, expand, intersection, normalize, subset, union } from '.';
import {
difference,
equal,
expand,
intersection,
normalize,
subset,
union,
} from '.';

function test1<A, B>(fn: (a: A) => B, tests: [A, B][]) {
for (const [a, b] of tests) {
Expand Down Expand Up @@ -50,6 +58,39 @@ test2(difference, [
['b,2', 'a,1', 'b,2'],
]);

test2(equal, [
['1', '1', true],
['1', '1-2', false],
['1', '2', false],
['1', '2-100', false],
['1', '3', false],
['1,5', '1-5', false],
['1-2', '1', false],
['1-2', '1-2', true],
['1-2', '2', false],
['1-2,3,4-5', '1-5', true],
['1-2,4-5', '1-5', false],
['1-3', '2-4', false],
['1-5', '1,5', false],
['1-5', '1-2,4-5', false],
['1-6', '1,3,6', false],
['1-6', '1,4', false],
['1-6', '1,6', false],
['1-6', '2,4', false],
['1-6', '2-5', false],
['2', '1', false],
['2-100', '1', false],
['2-4', '1-3', false],
['3', '1', false],
['a', 'a', true],
['a', 'b', false],
['a,1', 'b,1', false],
['a,1', 'b,2', false],
['b', 'a', false],
['b,1', 'a,1', false],
['b,2', 'a,1', false],
]);

test1(expand, [
[',', []],
[',,', []],
Expand Down

0 comments on commit fbd7581

Please sign in to comment.