Skip to content

Commit

Permalink
fix(typings): Fix typings of Set extensions
Browse files Browse the repository at this point in the history
Changes:

* Add demo
* Update docs
  • Loading branch information
motss committed Oct 10, 2019
1 parent 24652d4 commit 73f4cd5
Show file tree
Hide file tree
Showing 112 changed files with 387 additions and 238 deletions.
1 change: 1 addition & 0 deletions demo/main.ts
Expand Up @@ -9,3 +9,4 @@ import './number.js';
import './object.js';
import './promise.js';
import './regexp.js';
import './set.js';
53 changes: 53 additions & 0 deletions demo/set.ts
@@ -0,0 +1,53 @@
import { extend } from '../src/index.js';
import {
difference,
from,
intersection,
isDisjoint,
isEmpty,
isSet,
isSubset,
isSuperset,
iter,
len,
of,
symmetricDifference,
toArray,
union,
} from '../src/set.js';

extend({
set: [
difference,
from,
intersection,
isDisjoint,
isEmpty,
isSet,
isSubset,
isSuperset,
iter,
len,
of,
symmetricDifference,
toArray,
union,
],
});

console.log([
Set.prototype.difference,
Set.from,
Set.prototype.intersection,
Set.prototype.isDisjoint,
Set.prototype.isEmpty,
Set.isSet,
Set.prototype.isSubset,
Set.prototype.isSuperset,
Set.prototype.iter,
Set.prototype.len,
Set.of,
Set.prototype.symmetricDifference,
Set.prototype.toArray,
Set.prototype.union,
].every(n => 'function' === typeof(n)));
2 changes: 1 addition & 1 deletion src/date/difference.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { dateIsDate } from './is-date';
import { dateIsDate } from './is-date.js';

export function utilDateDifference(a: Date, b: Date) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/date/is-after.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { utilDateDifference } from './difference';
import { utilDateDifference } from './difference.js';

interface IsAfter {
isAfter(other: Date): boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/date/is-before.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { utilDateDifference } from './difference';
import { utilDateDifference } from './difference.js';

interface IsBefore {
isBefore(other: Date): boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/number/check-int.ts
@@ -1,4 +1,4 @@
import { numberIsNumber } from './is-number';
import { numberIsNumber } from './is-number.js';

export function checkInt(n: number | null | undefined, name: string) {
if (numberIsNumber(n)) return Number(n);
Expand Down
2 changes: 1 addition & 1 deletion src/number/div-mod-floor.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { utilDivFloor } from './div-floor';
import { utilDivFloor } from './div-floor.js';

interface DivModFloor {
divModFloor(divisor: number): number[];
Expand Down
2 changes: 1 addition & 1 deletion src/number/lcm.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { utilGcd } from './gcd';
import { utilGcd } from './gcd.js';

interface Lcm {
lcm(divisor: number): number;
Expand Down
2 changes: 1 addition & 1 deletion src/number/mod-floor.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { utilDivFloor } from './div-floor';
import { utilDivFloor } from './div-floor.js';

interface ModFloor {
modFloor(divisor: number): number;
Expand Down
2 changes: 1 addition & 1 deletion src/number/range.ts
@@ -1,5 +1,5 @@
import { PrototypeStruct } from '..';
import { checkInt } from './check-int';
import { checkInt } from './check-int.js';

interface Range {
range(start: number, end?: number, step?: number): number[];
Expand Down
22 changes: 21 additions & 1 deletion src/set/API_REFERENCE.md
@@ -1,4 +1,24 @@
# API Reference
# API Reference <!-- omit in toc -->

## Table of contents <!-- omit in toc -->

- [Set](#set)
- [Constructor](#constructor)
- [from(setEntries[, mapFn])](#fromsetentries-mapfn)
- [isSet(x)](#issetx)
- [of(element0[, element1[, ...[, elementN]]])](#ofelement0-element1--elementn)
- [Prototype](#prototype)
- [difference(other)](#differenceother)
- [intersection(other)](#intersectionother)
- [isDisjoint(other)](#isdisjointother)
- [isEmpty()](#isempty)
- [isSubset(other)](#issubsetother)
- [isSuperset(other)](#issupersetother)
- [iter()](#iter)
- [len()](#len)
- [symmetricDifference(other)](#symmetricdifferenceother)
- [toArray()](#toarray)
- [union(other)](#unionother)

## Set

Expand Down
66 changes: 63 additions & 3 deletions src/set/README.md
Expand Up @@ -12,9 +12,69 @@
## Table of contents <!-- omit in toc -->

- [Usage](#Usage)
- [API Reference](#API-Reference)
- [License](#License)
- [Usage](#usage)
- [Available extensions](#available-extensions)
- [Constructor](#constructor)
- [Prototype](#prototype)
- [License](#license)

## Usage

```ts
import { extend } from 'jsmodern';
import {
difference,
from,
intersection,
isDisjoint,
isEmpty,
isSet,
isSubset,
isSuperset,
iter,
len,
of,
symmetricDifference,
toArray,
union,
} from 'jsmodern/dist/set.js';

extend({
set: [
difference,
from,
intersection,
isDisjoint,
isEmpty,
isSet,
isSubset,
isSuperset,
iter,
len,
of,
symmetricDifference,
toArray,
union,
],
});

console.log([
Set.prototype.difference,
Set.from,
Set.prototype.intersection,
Set.prototype.isDisjoint,
Set.prototype.isEmpty,
Set.isSet,
Set.prototype.isSubset,
Set.prototype.isSuperset,
Set.prototype.iter,
Set.prototype.len,
Set.of,
Set.prototype.symmetricDifference,
Set.prototype.toArray,
Set.prototype.union,
].every(n => 'function' === typeof(n)));
```

## Available extensions

Expand Down
9 changes: 5 additions & 4 deletions src/set/difference.ts
@@ -1,7 +1,10 @@
import { PrototypeStruct } from '..';
import { utilIsSet } from './is-set';

export type DifferenceFn<T> = (other: Set<T>) => T[];
interface Difference<T> {
difference(other: Set<T>): T[];
}

export const difference: PrototypeStruct = {
label: 'difference',
fn: function setDifference<T>(other: Set<T>): T[] {
Expand All @@ -24,7 +27,5 @@ export const difference: PrototypeStruct = {
};

declare global {
interface Set<T> {
difference: DifferenceFn<T>;
}
interface Set<T> extends Difference<T> {}
}
2 changes: 1 addition & 1 deletion src/set/from.ts
Expand Up @@ -4,7 +4,7 @@ type FromSetFn<T> = (n: T) => T;
export interface SetFrom {
from<T>(setEntry: T[], mapFn?: FromSetFn<T>): Set<T>;
}
export type FromFn<T> = (setEntry: T[], mapFn?: FromSetFn<T>) => Set<T>;

export const from: PrototypeStruct = {
isStatic: true,
label: 'from',
Expand Down
11 changes: 6 additions & 5 deletions src/set/intersection.ts
@@ -1,7 +1,10 @@
import { PrototypeStruct } from '..';
import { utilIsSet } from './is-set';
import { utilIsSet } from './is-set.js';

interface Intersection<T> {
intersection(other: Set<T>): T[];
}

export type IntersectionFn<T> = (other: Set<T>) => T[];
export const intersection: PrototypeStruct = {
label: 'intersection',
fn: function setIntersection<T>(other: Set<T>): T[] {
Expand All @@ -20,7 +23,5 @@ export const intersection: PrototypeStruct = {
};

declare global {
interface Set<T> {
intersection: IntersectionFn<T>;
}
interface Set<T> extends Intersection<T> {}
}
11 changes: 6 additions & 5 deletions src/set/is-disjoint.ts
@@ -1,7 +1,10 @@
import { PrototypeStruct } from '..';
import { utilIsSet } from './is-set';
import { utilIsSet } from './is-set.js';

interface IsDisjoint<T> {
isDisjoint(other: Set<T>): boolean;
}

export type IsDisjointFn<T> = (other: Set<T>) => boolean;
export const isDisjoint: PrototypeStruct = {
label: 'isDisjoint',
fn: function setIsDisjoint<T>(other: Set<T>): boolean {
Expand All @@ -18,7 +21,5 @@ export const isDisjoint: PrototypeStruct = {
};

declare global {
interface Set<T> {
isDisjoint: IsDisjointFn<T>;
}
interface Set<T> extends IsDisjoint<T> {}
}
10 changes: 6 additions & 4 deletions src/set/is-empty.ts
@@ -1,6 +1,9 @@
import { PrototypeStruct } from '..';

export type IsEmptyFn = () => boolean;
interface IsEmpty {
isEmpty(): boolean;
}

export const isEmpty: PrototypeStruct = {
label: 'isEmpty',
fn: function setIsEmpty<T>(): boolean {
Expand All @@ -11,7 +14,6 @@ export const isEmpty: PrototypeStruct = {
};

declare global {
interface Set<T> {
isEmpty: IsEmptyFn;
}
// tslint:disable-next-line: no-empty-interface
interface Set<T> extends IsEmpty {}
}
10 changes: 6 additions & 4 deletions src/set/is-set.ts
Expand Up @@ -4,15 +4,17 @@ export function utilIsSet<T>(x: any): x is Set<T> {
return null == x ? false : 'object' === typeof(x) && 'Set' === x.constructor.name;
}

export type IsSetFn = (x: any) => boolean;
interface IsSet {
isSet(x: any): boolean;
}

export const isSet: PrototypeStruct = {
isStatic: true,
label: 'isSet',
fn: utilIsSet,
};

declare global {
interface SetConstructor {
isSet: IsSetFn;
}
// tslint:disable-next-line: no-empty-interface
interface SetConstructor extends IsSet {}
}
11 changes: 6 additions & 5 deletions src/set/is-subset.ts
@@ -1,7 +1,10 @@
import { PrototypeStruct } from '..';
import { utilIsSet } from './is-set';
import { utilIsSet } from './is-set.js';

interface IsSubset<T> {
isSubset(other: Set<T>): boolean;
}

export type IsSubsetFn<T> = (other: Set<T>) => boolean;
export const isSubset: PrototypeStruct = {
label: 'isSubset',
fn: function setIsSubset<T>(other: Set<T>): boolean {
Expand All @@ -18,7 +21,5 @@ export const isSubset: PrototypeStruct = {
};

declare global {
interface Set<T> {
isSubset: IsSubsetFn<T>;
}
interface Set<T> extends IsSubset<T> {}
}
11 changes: 6 additions & 5 deletions src/set/is-superset.ts
@@ -1,7 +1,10 @@
import { PrototypeStruct } from '..';
import { utilIsSet } from './is-set';
import { utilIsSet } from './is-set.js';

interface IsSuperset<T> {
isSuperset(other: Set<T>): boolean;
}

export type IsSupersetFn<T> = (other: Set<T>) => boolean;
export const isSuperset: PrototypeStruct = {
label: 'isSuperset',
fn: function setIsSuperset<T>(other: Set<T>): boolean {
Expand All @@ -18,7 +21,5 @@ export const isSuperset: PrototypeStruct = {
};

declare global {
interface Set<T> {
isSuperset: IsSupersetFn<T>;
}
interface Set<T> extends IsSuperset<T> {}
}
9 changes: 5 additions & 4 deletions src/set/iter.ts
@@ -1,6 +1,9 @@
import { PrototypeStruct } from '..';

export type IterFn<T> = () => IterableIterator<T>;
interface Iter<T> {
iter(): IterableIterator<T>;
}

export const iter: PrototypeStruct = {
label: 'iter',
fn: function setIter<T>(): IterableIterator<T> {
Expand All @@ -11,7 +14,5 @@ export const iter: PrototypeStruct = {
};

declare global {
interface Set<T> {
iter: IterFn<T>;
}
interface Set<T> extends Iter<T> {}
}

0 comments on commit 73f4cd5

Please sign in to comment.