Skip to content

Commit 834db31

Browse files
committed
v1.0 release with async iterator fix (huan/memory-card#39)
1 parent 774dcaf commit 834db31

12 files changed

+124
-47
lines changed

.eslintrc.js .eslintrc.cjs

File renamed without changes.

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ export interface AsyncMapLike<K = any, V = any> {
5353

5454
}
5555
```
56+
5657
## History
5758

58-
### master
59+
### master v1.0 (Nov 27, 2021)
60+
61+
1. Remove sync interface: `[Symbol.toStringTag]()` and `[Symbol.iterator]()`
62+
1. Add async interface: `[Symbol.asyncIterator]()`
5963

6064
### v0.2 (July 25, 2020)
6165

package.json

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{
22
"name": "async-map-like",
3-
"version": "0.2.5",
3+
"version": "1.0.0",
44
"description": "ES6 Map TypeScript Interface with Async Support",
5-
"main": "dist/src/mod.js",
6-
"typings": "dist/src/mod.d.ts",
5+
"type": "module",
6+
"exports": {
7+
".": {
8+
"import": "./dist/esm/src/mod.js",
9+
"require": "./dist/cjs/src/mod.js"
10+
}
11+
},
12+
"typings": "./dist/esm/src/mod.d.ts",
713
"engines": {
8-
"node": ">= 12"
14+
"node": ">=16",
15+
"npm": ">=7"
916
},
1017
"keywords": [
1118
"async",
@@ -15,14 +22,15 @@
1522
],
1623
"scripts": {
1724
"clean": "shx rm -fr dist/*",
18-
"dist": "npm run clean && tsc",
19-
"pack": "npm pack",
20-
"lint": "npm run lint:es && npm run lint:ts",
21-
"lint:ts": "tsc --noEmit",
22-
"test": "npm run lint && npm run test:unit",
23-
"test:unit": "blue-tape -r ts-node/register \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
25+
"dist": "npm-run-all clean build dist:commonjs",
26+
"build": "tsc && tsc -p tsconfig.cjs.json",
27+
"dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
28+
"lint": "npm-run-all lint:es lint:ts",
29+
"lint:ts": "tsc --isolatedModules --noEmit",
30+
"test": "npm-run-all lint test:unit",
31+
"test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
2432
"test:pack": "bash -x scripts/npm-pack-testing.sh",
25-
"lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'"
33+
"lint:es": "eslint --ignore-pattern tests/fixtures/ \"{bin,examples,scripts,src,tests}/**/*.ts\""
2634
},
2735
"repository": {
2836
"type": "git",
@@ -35,13 +43,10 @@
3543
},
3644
"homepage": "https://github.com/huan/async-map-like#readme",
3745
"devDependencies": {
38-
"@chatie/eslint-config": "^0.12.1",
46+
"@chatie/eslint-config": "^1.0.4",
3947
"@chatie/git-scripts": "^0.6.2",
4048
"@chatie/semver": "^0.4.7",
41-
"@chatie/tsconfig": "^0.10.1",
42-
"pkg-jq": "^0.2.4",
43-
"shx": "^0.3.2",
44-
"tstest": "^0.4.10"
49+
"@chatie/tsconfig": "^4.5.3"
4550
},
4651
"dependencies": {},
4752
"git": {

scripts/npm-pack-testing.sh

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,56 @@
22
set -e
33

44
npm run dist
5-
npm run pack
5+
npm pack
66

77
TMPDIR="/tmp/npm-pack-testing.$$"
88
mkdir "$TMPDIR"
99
mv *-*.*.*.tgz "$TMPDIR"
1010
cp tests/fixtures/smoke-testing.ts "$TMPDIR"
1111

1212
cd $TMPDIR
13+
1314
npm init -y
1415
npm install *-*.*.*.tgz \
1516
@types/node \
1617
typescript \
1718

19+
#
20+
# CommonJS
21+
#
22+
./node_modules/.bin/tsc \
23+
--esModuleInterop \
24+
--lib esnext \
25+
--noEmitOnError \
26+
--noImplicitAny \
27+
--skipLibCheck \
28+
--target es5 \
29+
--module CommonJS \
30+
--moduleResolution node \
31+
smoke-testing.ts
32+
33+
echo
34+
echo "CommonJS: pack testing..."
35+
node smoke-testing.js
36+
37+
#
38+
# ES Modules
39+
#
40+
41+
# https://stackoverflow.com/a/59203952/1123955
42+
echo "`jq '.type="module"' package.json`" > package.json
43+
1844
./node_modules/.bin/tsc \
19-
--lib esnext,dom \
45+
--esModuleInterop \
46+
--lib esnext \
2047
--noEmitOnError \
2148
--noImplicitAny \
49+
--skipLibCheck \
50+
--target es2020 \
51+
--module es2020 \
52+
--moduleResolution node \
2253
smoke-testing.ts
2354

55+
echo
56+
echo "ES Module: pack testing..."
2457
node smoke-testing.js

src/async-map-like.spec.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

33
import { test } from 'tstest'
44

5-
import { AsyncMapLike } from './async-map-like'
5+
import type { AsyncMapLike } from './async-map-like.js'
66

77
test('AsyncMapLike Interface via object', async (t) => {
88
const mapCollection = {
@@ -23,10 +23,10 @@ test('AsyncMapLike Interface via object', async (t) => {
2323
}
2424

2525
const mapIterable = {
26-
[Symbol.iterator] : () : AsyncIterableIterator<[any, any]> => { return {} as any },
2726
entries : () : AsyncIterableIterator<[any, any]> => { return {} as any },
2827
keys : () : AsyncIterableIterator<any> => { return {} as any },
2928
values : () : AsyncIterableIterator<any> => { return {} as any },
29+
[Symbol.asyncIterator]: () : AsyncIterableIterator<[any, any]> => { return {} as any },
3030
}
3131

3232
const mapLike: AsyncMapLike<any, any> = {
@@ -67,12 +67,10 @@ test('AsyncMapLike Interface via class', async (t) => {
6767
/**
6868
* Iterables
6969
*/
70-
[Symbol.iterator] () : AsyncIterableIterator<[string, number]> { return {} as any }
7170
entries () : AsyncIterableIterator<[string, number]> { return {} as any }
7271
keys () : AsyncIterableIterator<string> { return {} as any }
7372
values () : AsyncIterableIterator<number> { return {} as any }
74-
75-
get [Symbol.toStringTag] () { return 'test' }
73+
[Symbol.asyncIterator] () : AsyncIterableIterator<[any, any]> { return {} as any }
7674

7775
}
7876

@@ -108,17 +106,25 @@ test('AsyncMapLike Interface via class generic', async (t) => {
108106
get size () { return Promise.resolve(42) }
109107

110108
/**
111-
* Iterables
109+
* AsyncIterables
112110
*/
113-
[Symbol.iterator] () : AsyncIterableIterator<[K, V]> { return {} as any }
114111
entries () : AsyncIterableIterator<[K, V]> { return {} as any }
115112
keys () : AsyncIterableIterator<K> { return {} as any }
116113
values () : AsyncIterableIterator<V> { return {} as any }
117-
118-
get [Symbol.toStringTag] () { return 'test' }
114+
[Symbol.asyncIterator] () : AsyncIterableIterator<[any, any]> {
115+
return {
116+
[Symbol.asyncIterator]: this[Symbol.asyncIterator],
117+
next: () => Promise.resolve({ done: true, value: undefined }),
118+
}
119+
}
119120

120121
}
121122

122123
const mapLike = new TestAsyncMapLike<string, number>()
123124
t.ok(mapLike, 'should be implement-able from AsyncMapLike')
125+
126+
/**
127+
* Just check for the types
128+
*/
129+
for await (const kv of mapLike) { void kv }
124130
})

src/async-map-like.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/* eslint-disable no-use-before-define */
2+
import type { MapLike } from './map-like.js'
3+
4+
type MapLikeNoSync<K, V> = Omit<
5+
MapLike<K, V>,
6+
typeof Symbol.toStringTag | typeof Symbol.iterator
7+
>
8+
19
type IteratorMethod = 'entries' | 'keys' | 'values'
210
type GetMethod = 'get'
311
type AnyFunction = (...args: any) => any
@@ -29,6 +37,23 @@ type Asyncify<NAME, T> = T extends AnyFunction ?
2937
*/
3038
: Promise<T>
3139

32-
export type AsyncMapLike<K, V> = {
33-
[N in keyof Map<K, V>]: Asyncify<N, Map<K, V>[N]>
40+
type AsyncMapBase<K, V> = {
41+
[N in keyof MapLikeNoSync<K, V>]: Asyncify<N, MapLikeNoSync<K, V>[N]>
42+
}
43+
44+
interface AsyncMapIterator<K, V> {
45+
/**
46+
* Huan(202111): we have removed the `[Symbol.iterator]`, and add the below `[Symbol.asyncIterator]` for Async
47+
*/
48+
[Symbol.asyncIterator]: () => AsyncifyIterator<
49+
ReturnType<
50+
Map<K, V>[typeof Symbol.iterator]
51+
>
52+
>
53+
}
54+
55+
type AsyncMapLike<K, V> = AsyncMapBase<K, V> & AsyncMapIterator<K, V>
56+
57+
export type {
58+
AsyncMapLike,
3459
}

src/map-like.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

33
import { test } from 'tstest'
44

5-
import { MapLike } from './map-like'
5+
import type { MapLike } from './map-like.js'
66

77
test('MapLike Interface', async (t) => {
88
const mapLike: MapLike<any, any> = new Map<any, any>()

src/mod.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { VERSION } from './version'
1+
export { VERSION } from './version.js'
22

3-
export { MapLike } from './map-like'
4-
export { AsyncMapLike } from './async-map-like'
3+
export type { MapLike } from './map-like.js'
4+
export type { AsyncMapLike } from './async-map-like.js'

src/version.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

33
import { test } from 'tstest'
44

5-
import { VERSION } from './version'
5+
import { VERSION } from './version.js'
66

7-
test('Make sure the VERSION is fresh in source code', async (t) => {
7+
test('Make sure the VERSION is fresh in source code', async t => {
88
t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM')
99
})

tests/integration.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#!/usr/bin/env ts-node
2-
import test from 'blue-tape'
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2+
import { test } from 'tstest'
33

4-
import {
4+
import type {
55
MapLike,
66
AsyncMapLike,
7-
} from '../src/mod'
7+
} from '../src/mod.js'
88

99
test('integrate testing', async t => {
1010
let mapLike : undefined | MapLike<any, any>

tsconfig.cjs.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"outDir": "dist/cjs",
6+
},
7+
}

tsconfig.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
{
22
"extends": "@chatie/tsconfig",
33
"compilerOptions": {
4-
"outDir": "dist",
5-
"target": "es6"
4+
"outDir": "dist/esm",
65
},
76
"exclude": [
87
"node_modules/",
98
"dist/",
109
"tests/fixtures/",
1110
],
1211
"include": [
13-
"app/**/*.ts",
1412
"bin/*.ts",
15-
"bot/**/*.ts",
1613
"examples/**/*.ts",
1714
"scripts/**/*.ts",
1815
"src/**/*.ts",

0 commit comments

Comments
 (0)