Skip to content

Commit c245deb

Browse files
committed
feat: Add typescript type definitions
1 parent 9a237c7 commit c245deb

File tree

9 files changed

+530
-68
lines changed

9 files changed

+530
-68
lines changed

index.d.ts

Lines changed: 510 additions & 0 deletions
Large diffs are not rendered by default.

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ exports.oneOf = require('./oneOf')
33
exports.tuple = require('./tuple')
44

55
exports.int = require('./int')
6+
exports.float = require('./float')
67
exports.word = require('./word')
78
exports.bool = require('./bool')
89
exports.datestr = require('./datestr')
9-
exports.float = require('./float')

index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { default as oneOf } from './oneOf'
33
export { default as tuple } from './tuple'
44

55
export { default as int } from './int'
6+
export { default as float } from './float'
67
export { default as word } from './word'
78
export { default as bool } from './bool'
89
export { default as datestr } from './datestr'
9-
export { default as float } from './float'

index.test-d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expectType } from 'tsd'
2+
import { Input, int, word, tuple, oneOf } from '.'
3+
4+
// ## tuple
5+
// ### function items
6+
expectType<[number]>(tuple(23, [int]))
7+
expectType<[number, string]>(tuple(23, [int, word]))
8+
expectType<[number, string, number]>(tuple(23, [int, word, int]))
9+
10+
// ## currying
11+
expectType<(input: Input) => [number]>(tuple([int]))
12+
expectType<(input: Input) => number>(oneOf([2, 3]))

internal/displayType.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

internal/typeOf.js

Whitespace-only changes.

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
Generate fake data deterministically from a given input
44

5-
_Docs and type definitions coming soon_
5+
_Docs coming soon_

tests/tuple.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
const tap = require('tap')
22
const { tuple } = require('..')
33

4-
tap.test('array items', t => {
5-
const result = tuple(23, [[(v, suffix) => [v, suffix].join(''), '!']])
6-
7-
t.match(result, [/^\d+!$/])
8-
t.end()
9-
})
10-
11-
tap.test('invalid items', t => {
12-
t.throws(() => tuple(23, [2]))
13-
t.throws(() => tuple(23, [[3]]))
14-
t.end()
15-
})
16-
174
tap.test('currying', t => {
185
const fn = v => `${v}!`
196
t.deepEquals(tuple(23, [fn]), tuple([fn])(23))

tuple.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,25 @@
11
var hash = require('./hash')
2-
var displayType = require('./internal/displayType')
32

43
module.exports = function tuple(a, b) {
54
return a != null && b != null ? tupleMain(a, b) : tupleCurried(a)
65
}
76

8-
function tupleMain(inputs, items) {
7+
function tupleMain(inputs, fns) {
98
var id = hash(inputs)
10-
var n = items.length
9+
var n = fns.length
1110
var i = -1
1211
var results = []
1312

1413
while (++i < n) {
1514
id = hash(id)
16-
results.push(callItem(items[i], id))
15+
results.push(fns[i](id))
1716
}
1817

1918
return results
2019
}
2120

22-
function callItem(item, id) {
23-
var fn
24-
25-
if (typeof item === 'function') {
26-
return item(id)
27-
}
28-
29-
if (Array.isArray(item)) {
30-
fn = item[0]
31-
32-
if (typeof fn === 'function') {
33-
return item[0].apply(null, [id].concat(item.slice(1)))
34-
}
35-
}
36-
37-
throw new Error(
38-
'tuple() was given an array item of type ' +
39-
displayType(item) +
40-
'. Each item in the array given to tuple() must either be a function, or an array with a function as its first item.'
41-
)
42-
}
43-
44-
function tupleCurried(items) {
21+
function tupleCurried(fns) {
4522
return function tupleCurriedFn(inputs) {
46-
return tupleMain(inputs, items)
23+
return tupleMain(inputs, fns)
4724
}
4825
}

0 commit comments

Comments
 (0)