Skip to content

Commit 05684dc

Browse files
committed
CachedIterable copied from fluent 0.6.4
0 parents  commit 05684dc

16 files changed

+3397
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
cached-iterable.js
3+
compat.js

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
Makefile
3+
*_config.js
4+
eslint_*

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sudo: false
2+
language: node_js
3+
script: make all
4+
node_js: "8.9"
5+
cache:
6+
directories: node_modules

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export SHELL := /bin/bash
2+
export PATH := $(CURDIR)/node_modules/.bin:$(PATH)
3+
4+
SOURCES := $(wildcard src/*)
5+
VERSION := $(shell node -pe "require('./package.json').version")
6+
OK := \033[32;01m✓\033[0m
7+
8+
PACKAGE := cached-iterable
9+
GLOBAL := CachedIterable
10+
11+
# The default target.
12+
all: lint test build
13+
14+
lint:
15+
@eslint --config $(CURDIR)/eslint_src.json --max-warnings 0 src/
16+
@eslint --config $(CURDIR)/eslint_test.json --max-warnings 0 test/
17+
@echo -e " $(OK) $@"
18+
19+
.PHONY: test
20+
test:
21+
@mocha --recursive --ui tdd \
22+
--require mocha_config \
23+
test/**/*_test.js
24+
25+
build: $(PACKAGE).js compat.js
26+
27+
$(PACKAGE).js: $(SOURCES)
28+
@rollup $(CURDIR)/src/index.mjs \
29+
--config $(CURDIR)/bundle_config.js \
30+
--banner "/* $(PACKAGE)@$(VERSION) */" \
31+
--amd.id $(PACKAGE) \
32+
--name $(GLOBAL) \
33+
--output.file $@
34+
@echo -e " $(OK) $@ built"
35+
36+
compat.js: $(SOURCES)
37+
@rollup $(CURDIR)/src/index.mjs \
38+
--config $(CURDIR)/compat_config.js \
39+
--banner "/* $(PACKAGE)@$(VERSION) */" \
40+
--amd.id $(PACKAGE) \
41+
--name $(GLOBAL) \
42+
--output.file $@
43+
@echo -e " $(OK) $@ built"
44+
45+
clean:
46+
@rm -f $(PACKAGE).js compat.js
47+
@echo -e " $(OK) clean"

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# cached-iterable
2+
3+
`cached-iterable` exposes the `CachedItearble` class which implements the
4+
[iterable protocol][].
5+
6+
You can wrap any iterable in these classes to create a new iterable which
7+
caches the yielded elements. This is useful for iterating over an iterable many
8+
times without depleting it.
9+
10+
[iterable protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol
11+
12+
## Installation
13+
14+
`cached-iterable` can be used both on the client-side and the server-side. You
15+
can install it from the npm registry or use it as a standalone script (as the
16+
`CachedIterable` global).
17+
18+
npm install cached-iterable
19+
20+
## How to use
21+
22+
```js
23+
import assert from "assert";
24+
import {CachedIterable} from "cached-iterable";
25+
26+
function * countdown(i) {
27+
while (i--) {
28+
yield i;
29+
}
30+
}
31+
32+
let numbers = new CachedIterable(countdown(3));
33+
34+
// `numbers` can be iterated over multiple times.
35+
assert.deepEqual([...numbers], [3, 2, 1, 0]);
36+
assert.deepEqual([...numbers], [3, 2, 1, 0]);
37+
```
38+
39+
## Compatibility
40+
41+
For legacy browsers, the `compat` build has been transpiled using Babel's [env
42+
preset][]. It requires the regenerator runtime provided by [babel-polyfill][].
43+
44+
```javascript
45+
import {CachedIterable} from 'cached-iterable/compat';
46+
```
47+
48+
[env preset]: https://babeljs.io/docs/plugins/preset-env/
49+
[babel-polyfill]: https://babeljs.io/docs/usage/polyfill/

babel_config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
babelrc: false,
3+
presets: [
4+
["@babel/preset-env", {
5+
// Cf. https://github.com/rollup/rollup-plugin-babel#modules
6+
modules: false,
7+
targets: {
8+
browsers: [
9+
">1%",
10+
"last 4 versions",
11+
"Firefox ESR",
12+
"not ie < 9"
13+
]
14+
}
15+
}]
16+
]
17+
};

bundle_config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
output: {
3+
format: 'umd',
4+
},
5+
acorn: {
6+
ecmaVersion: 9,
7+
},
8+
};

compat_config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import babel from 'rollup-plugin-babel';
2+
import babelConfig from './babel_config';
3+
4+
export default {
5+
output: {
6+
format: 'umd'
7+
},
8+
plugins: [
9+
babel(babelConfig),
10+
],
11+
};

eslint_src.json

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"parser": "babel-eslint",
3+
"parserOptions": {
4+
"ecmaVersion": 9,
5+
"sourceType": "module"
6+
},
7+
"env": {
8+
"es6": true
9+
},
10+
"rules": {
11+
"valid-typeof": 2,
12+
"no-console": 1,
13+
"no-dupe-args": 2,
14+
"no-dupe-keys": 2,
15+
"no-duplicate-case": 2,
16+
"no-extra-semi": 1,
17+
"no-func-assign": 2,
18+
"func-call-spacing": [
19+
2,
20+
"never"
21+
],
22+
"no-unreachable": 1,
23+
"no-unexpected-multiline": 2,
24+
"no-sparse-arrays": 2,
25+
"complexity": [
26+
1,
27+
18
28+
],
29+
"dot-location": [
30+
2,
31+
"property"
32+
],
33+
"no-implied-eval": 2,
34+
"no-loop-func": 2,
35+
"no-magic-numbers": [
36+
1,
37+
{
38+
"ignore": [
39+
-1,
40+
0,
41+
1,
42+
2
43+
]
44+
}
45+
],
46+
"no-useless-call": 2,
47+
"no-useless-concat": 2,
48+
"no-delete-var": 2,
49+
"no-shadow": 2,
50+
"no-undef": 2,
51+
"no-undef-init": 2,
52+
"no-unused-vars": 1,
53+
"consistent-return": 1,
54+
"curly": [
55+
2,
56+
"multi-line"
57+
],
58+
"eqeqeq": 2,
59+
"no-extend-native": 2,
60+
"no-global-assign": 2,
61+
"no-extra-bind": 2,
62+
"no-redeclare": 2,
63+
"array-bracket-spacing": 2,
64+
"brace-style": [
65+
1,
66+
"1tbs"
67+
],
68+
"no-mixed-spaces-and-tabs": 2,
69+
"no-tabs": 2,
70+
"prefer-arrow-callback": 1,
71+
"prefer-const": 2,
72+
"prefer-template": 2,
73+
"prefer-spread": 2,
74+
"quotes": [
75+
2,
76+
"double",
77+
{
78+
"avoidEscape": true
79+
}
80+
],
81+
"max-depth": [
82+
2,
83+
6
84+
],
85+
"max-len": [
86+
2,
87+
80
88+
],
89+
"no-nested-ternary": 2,
90+
"no-unneeded-ternary": 2,
91+
"strict": [
92+
2,
93+
"global"
94+
],
95+
"indent": [
96+
2,
97+
4,
98+
{
99+
"SwitchCase": 1
100+
}
101+
],
102+
"no-useless-escape": 2,
103+
"no-duplicate-imports": 2,
104+
"no-unsafe-negation": 2,
105+
"no-use-before-define": [
106+
2,
107+
{
108+
"functions": false,
109+
"classes": false
110+
}
111+
],
112+
"space-infix-ops": 2,
113+
"no-constant-condition": [
114+
1,
115+
{
116+
"checkLoops": false
117+
}
118+
],
119+
"no-empty": 1,
120+
"use-isnan": 2,
121+
"dot-notation": 2,
122+
"no-caller": 2,
123+
"no-else-return": 2,
124+
"no-eval": 2,
125+
"no-implicit-globals": 2,
126+
"no-iterator": 2,
127+
"no-multi-spaces": 2,
128+
"no-multi-str": 2,
129+
"no-octal": 2,
130+
"no-proto": 2,
131+
"no-sequences": 2,
132+
"block-spacing": 2,
133+
"eol-last": 2,
134+
"key-spacing": 2,
135+
"no-trailing-spaces": 2,
136+
"prefer-rest-params": 2,
137+
"no-cond-assign": 2,
138+
"keyword-spacing": 2,
139+
"arrow-parens": [
140+
2,
141+
"as-needed"
142+
],
143+
"no-useless-return": 2,
144+
"semi": 2,
145+
"spaced-comment": [
146+
2,
147+
"always"
148+
]
149+
}
150+
}

eslint_test.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"parser": "babel-eslint",
3+
"parserOptions": {
4+
"ecmaVersion": 9,
5+
"sourceType": "module",
6+
"ecmaFeatures": {
7+
"jsx": true
8+
}
9+
},
10+
"env": {
11+
"es6": true,
12+
"node": true,
13+
"mocha": true
14+
},
15+
"plugins": [
16+
"mocha"
17+
],
18+
"rules": {
19+
"mocha/no-exclusive-tests": "error",
20+
"mocha/no-identical-title": "error"
21+
}
22+
}

mocha_config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
require("@babel/polyfill");
4+
require("@babel/register")({
5+
plugins: [
6+
"@babel/plugin-proposal-async-generator-functions",
7+
"@babel/plugin-transform-modules-commonjs"
8+
]
9+
});

0 commit comments

Comments
 (0)