Skip to content

Commit

Permalink
no implicit any
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Apr 2, 2019
1 parent 5d5b7ad commit fd91bc9
Show file tree
Hide file tree
Showing 40 changed files with 454 additions and 67 deletions.
14 changes: 14 additions & 0 deletions array.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@

/**
* Return the last element of an array. The element must exist
*
* @template T
* @param {Array<T>} arr
* @return {T}
*/
export const last = arr => arr[arr.length - 1]

/**
* @template T
* @return {Array<T>}
*/
export const create = () => []
8 changes: 8 additions & 0 deletions binary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
import * as binary from './binary.js'
import * as t from './testing.js'

/**
* @param {t.TestCase} tc
*/
export const testBitx = tc => {
for (let i = 1; i <= 32; i++) {
// @ts-ignore
t.assert(binary[`BIT${i}`] === (1 << (i - 1)), `BIT${i}=${1 << (i - 1)}`)
}
}

/**
* @param {t.TestCase} tc
*/
export const testBitsx = tc => {
t.assert(binary.BITS0 === 0)
for (let i = 1; i < 32; i++) {
const expected = (1 << i) - 1
// @ts-ignore
const have = binary[`BITS${i}`]
t.assert(have === expected, `BITS${i}=${have}=${expected}`)
}
Expand Down
11 changes: 10 additions & 1 deletion broadcastchannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import * as buffer from './buffer.js'
const channels = new Map()

class LocalStoragePolyfill {
/**
* @param {string} room
*/
constructor (room) {
this.room = room
/**
* @type {null|function({data:ArrayBuffer}):void}
*/
this.onmessage = null
addEventListener('storage', e => e.key === room && this.onmessage !== null && this.onmessage({ data: buffer.fromBase64(e.newValue || '') }))
}
Expand All @@ -38,9 +44,12 @@ const BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : Broa
* @return {Channel}
*/
const getChannel = room =>
map.setTfUndefined(channels, room, () => {
map.setIfUndefined(channels, room, () => {
const subs = new Set()
const bc = new BC(room)
/**
* @param {{data:ArrayBuffer}} e
*/
bc.onmessage = e => subs.forEach(sub => sub(e.data))
return {
bc, subs
Expand Down
14 changes: 13 additions & 1 deletion buffer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import * as string from './string.js'
import * as env from './environment.js'

/**
* @param {number} len
*/
export const createUint8ArrayFromLen = len => new Uint8Array(len)

/**
* Create Uint8Array with initial content from buffer
*
* @param {ArrayBuffer} buffer
* @param {number} byteOffset
* @param {number} length
*/
export const createUint8ArrayViewFromArrayBuffer = (buffer, byteOffset, length) => new Uint8Array(buffer, byteOffset, length)

/**
* Create Uint8Array with initial content from buffer
*
* @param {ArrayBuffer} buffer
*/
export const createUint8ArrayFromArrayBuffer = arraybuffer => new Uint8Array(arraybuffer)
export const createUint8ArrayFromArrayBuffer = buffer => new Uint8Array(buffer)

/* istanbul ignore next */
/**
Expand Down Expand Up @@ -48,6 +57,9 @@ const fromBase64Browser = s => {
return bytes
}

/**
* @param {string} s
*/
const fromBase64Node = s => new Uint8Array(Buffer.from(s, 'base64').buffer)

/* istanbul ignore next */
Expand Down
3 changes: 3 additions & 0 deletions buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as t from './testing.js'
import * as buffer from './buffer.js'
import * as prng from './prng.js'

/**
* @param {t.TestCase} tc
*/
export const testRepeatBase64Encoding = tc => {
const gen = tc.prng
const barr = prng.uint8Array(gen, 100000)
Expand Down
27 changes: 0 additions & 27 deletions console.js

This file was deleted.

11 changes: 11 additions & 0 deletions diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import { simpleDiff } from './diff.js'
import * as prng from './prng.js'
import * as t from './testing.js'

/**
* @param {string} a
* @param {string} b
* @param {{pos: number,remove:number,insert:string}} expected
*/
function runDiffTest (a, b, expected) {
let result = simpleDiff(a, b)
t.compare(result, expected)
}

/**
* @param {t.TestCase} tc
*/
export const testDiffing = tc => {
runDiffTest('abc', 'axc', { pos: 1, remove: 1, insert: 'x' })
runDiffTest('bc', 'xc', { pos: 0, remove: 1, insert: 'x' })
Expand All @@ -18,6 +26,9 @@ export const testDiffing = tc => {
runDiffTest('ax', 'axy', { pos: 2, remove: 0, insert: 'y' })
}

/**
* @param {t.TestCase} tc
*/
export const testRepeatDiffing = tc => {
const a = prng.word(tc.prng)
const b = prng.word(tc.prng)
Expand Down
19 changes: 17 additions & 2 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ export const append = (parent, nodes) => {
return parent
}

/**
* @param {EventTarget} el
* @param {string} name
* @param {EventListener} f
*/
export const addEventListener = (el, name, f) => el.addEventListener(name, f)

/**
* @param {EventTarget} el
* @param {string} name
* @param {EventListener} f
*/
export const removeEventListener = (el, name, f) => el.removeEventListener(name, f)

/**
* @param {Node} node
* @param {Array<pair.Pair<string,function>>} listeners
* @param {Array<pair.Pair<string,EventListener>>} listeners
* @return {Node}
*/
export const addEventListeners = (node, listeners) => {
Expand All @@ -64,7 +75,7 @@ export const addEventListeners = (node, listeners) => {

/**
* @param {Node} node
* @param {Array<pair.Pair<string,function>>} listeners
* @param {Array<pair.Pair<string,EventListener>>} listeners
* @return {Node}
*/
export const removeEventListeners = (node, listeners) => {
Expand Down Expand Up @@ -92,6 +103,10 @@ export const canvas = (width, height) => {
return c
}

/**
* @param {string} t
* @return {Text}
*/
export const text = t => document.createTextNode(t)

/**
Expand Down
55 changes: 52 additions & 3 deletions encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export const testGolangBinaryEncodingCompatibility = () => {
})
}

/**
* @template T
* @param {string} testname
* @param {function(encoding.Encoder, T):void} write
* @param {function(decoding.Decoder):T} read
* @param {T} val
* @param {boolean} doLog
*/
function test (testname, write, read, val, doLog = true) {
let encoder = encoding.createEncoder()
write(encoder, val)
Expand All @@ -56,14 +64,17 @@ function test (testname, write, read, val, doLog = true) {
if (doLog) {
t.describe(testname, ` utf8 encode: ${utf8ByteLength} bytes / binary encode: ${binaryByteLength} bytes`)
}
t.compareStrings(val + '', result + '')
t.compare(val, result)
return {
utf8ByteLength,
binaryByteLength
}
}

const testVarString = (s) => {
/**
* @param {string} s
*/
const testVarString = s => {
const encoder = encoding.createEncoder()
encoding.writeVarString(encoder, s)
const decoder = decoding.createDecoder(encoding.toBuffer(encoder))
Expand All @@ -84,16 +95,25 @@ export const testVarUintEncoding = tc => {
test('varUint of 2839012934', encoding.writeVarUint, decoding.readVarUint, 2839012934)
}

/**
* @param {t.TestCase} tc
*/
export const testRepeatVarUintEncoding = tc => {
const n = prng.int31(tc.prng, 0, (1 << 28) - 1)
test(`varUint of ${n}`, encoding.writeVarUint, decoding.readVarUint, n, false)
}

/**
* @param {t.TestCase} tc
*/
export const testRepeatPeekVarUintEncoding = tc => {
const n = prng.int31(tc.prng, 0, (1 << 28) - 1)
test(`varUint of ${n}`, encoding.writeVarUint, decoding.peekVarUint, n, false)
}

/**
* @param {t.TestCase} tc
*/
export const testStringEncoding = tc => {
testVarString('hello')
testVarString('test!')
Expand All @@ -105,9 +125,15 @@ export const testStringEncoding = tc => {
testVarString('😝') // surrogate length 4
}

/**
* @param {t.TestCase} tc
*/
export const testRepeatStringEncoding = tc =>
testVarString(prng.utf16String(tc.prng))

/**
* @param {t.TestCase} tc
*/
export const testSetMethods = tc => {
const encoder = encoding.createEncoder()
encoding.writeUint8(encoder, 1)
Expand All @@ -130,7 +156,15 @@ const defLen = 1000
const loops = 10000

/**
* @type {Array<any>}
* @typedef {Object} EncodingPair
* @property {function(decoding.Decoder):any} EncodingPair.read
* @property {function(encoding.Encoder,any):void} EncodingPair.write
* @property {function(prng.PRNG):any} EncodingPair.gen
*/

/**
* @template T
* @type {Array<EncodingPair>}
*/
const encodingPairs = [
{ read: decoder => decoding.readArrayBuffer(decoder, defLen), write: encoding.writeArrayBuffer, gen: gen => prng.arrayBuffer(gen, defLen) },
Expand All @@ -142,6 +176,9 @@ const encodingPairs = [
{ read: decoding.readVarUint, write: encoding.writeVarUint, gen: gen => prng.uint53(gen, 0, binary.BITS31) }
]

/**
* @param {t.TestCase} tc
*/
export const testRepeatRandomWrites = tc => {
t.describe(`Writing ${loops} random values`, `defLen=${defLen}`)
const gen = tc.prng
Expand Down Expand Up @@ -169,6 +206,9 @@ export const testRepeatRandomWrites = tc => {
t.compare(tailData, decoding.readTail(decoder))
}

/**
* @param {t.TestCase} tc
*/
export const testSetOnOverflow = tc => {
const encoder = encoding.createEncoder()
const initialLen = encoder.cbuf.byteLength
Expand All @@ -192,6 +232,9 @@ export const testSetOnOverflow = tc => {
t.assert(buffer.createUint8ArrayFromArrayBuffer(buf2)[initialLen + 1] === 7)
}

/**
* @param {t.TestCase} tc
*/
export const testCloneDecoder = tc => {
const encoder = encoding.createEncoder()
encoding.writeUint8(encoder, 12132)
Expand All @@ -206,6 +249,9 @@ export const testCloneDecoder = tc => {
t.compare(payload1, payload2)
}

/**
* @param {t.TestCase} tc
*/
export const testWriteBinaryEncoder = tc => {
const encoder = encoding.createEncoder()
encoding.writeUint16(encoder, 4)
Expand All @@ -218,6 +264,9 @@ export const testWriteBinaryEncoder = tc => {
t.assert(decoding.readUint16(decoder) === 4)
}

/**
* @param {t.TestCase} tc
*/
export const testOverflowStringDecoding = tc => {
const gen = tc.prng
const encoder = encoding.createEncoder()
Expand Down
14 changes: 12 additions & 2 deletions environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ const computeParamsBrowser = () => {
/* istanbul ignore next */
const computeParams = isNode ? computeParamsNode : computeParamsBrowser

/**
* @param {string} name
* @return {boolean}
*/
export const hasParam = name => computeParams().has(name)

/* istanbul ignore next */
export const getParam = (name, defaultVal = null) => computeParams().get(name) || defaultVal
/**
* @param {string} name
* @param {string} defaultVal
* @return {string}
*/
export const getParam = (name, defaultVal) => computeParams().get(name) || defaultVal
// export const getArgs = name => computeParams() && args

export const production = getParam('production', false)
export const production = getParam('production', '0') !== '0'
Loading

0 comments on commit fd91bc9

Please sign in to comment.