Skip to content

Commit

Permalink
CachedIterable copied from fluent 0.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed May 31, 2018
0 parents commit 05684dc
Show file tree
Hide file tree
Showing 16 changed files with 3,397 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
cached-iterable.js
compat.js
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
Makefile
*_config.js
eslint_*
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
script: make all
node_js: "8.9"
cache:
directories: node_modules
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export SHELL := /bin/bash
export PATH := $(CURDIR)/node_modules/.bin:$(PATH)

SOURCES := $(wildcard src/*)
VERSION := $(shell node -pe "require('./package.json').version")
OK := \033[32;01m✓\033[0m

PACKAGE := cached-iterable
GLOBAL := CachedIterable

# The default target.
all: lint test build

lint:
@eslint --config $(CURDIR)/eslint_src.json --max-warnings 0 src/
@eslint --config $(CURDIR)/eslint_test.json --max-warnings 0 test/
@echo -e " $(OK) $@"

.PHONY: test
test:
@mocha --recursive --ui tdd \
--require mocha_config \
test/**/*_test.js

build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.mjs \
--config $(CURDIR)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--output.file $@
@echo -e " $(OK) $@ built"

compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.mjs \
--config $(CURDIR)/compat_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--output.file $@
@echo -e " $(OK) $@ built"

clean:
@rm -f $(PACKAGE).js compat.js
@echo -e " $(OK) clean"
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# cached-iterable

`cached-iterable` exposes the `CachedItearble` class which implements the
[iterable protocol][].

You can wrap any iterable in these classes to create a new iterable which
caches the yielded elements. This is useful for iterating over an iterable many
times without depleting it.

[iterable protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol

## Installation

`cached-iterable` can be used both on the client-side and the server-side. You
can install it from the npm registry or use it as a standalone script (as the
`CachedIterable` global).

npm install cached-iterable

## How to use

```js
import assert from "assert";
import {CachedIterable} from "cached-iterable";

function * countdown(i) {
while (i--) {
yield i;
}
}

let numbers = new CachedIterable(countdown(3));

// `numbers` can be iterated over multiple times.
assert.deepEqual([...numbers], [3, 2, 1, 0]);
assert.deepEqual([...numbers], [3, 2, 1, 0]);
```

## Compatibility

For legacy browsers, the `compat` build has been transpiled using Babel's [env
preset][]. It requires the regenerator runtime provided by [babel-polyfill][].

```javascript
import {CachedIterable} from 'cached-iterable/compat';
```

[env preset]: https://babeljs.io/docs/plugins/preset-env/
[babel-polyfill]: https://babeljs.io/docs/usage/polyfill/
17 changes: 17 additions & 0 deletions babel_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
babelrc: false,
presets: [
["@babel/preset-env", {
// Cf. https://github.com/rollup/rollup-plugin-babel#modules
modules: false,
targets: {
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9"
]
}
}]
]
};
8 changes: 8 additions & 0 deletions bundle_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
output: {
format: 'umd',
},
acorn: {
ecmaVersion: 9,
},
};
11 changes: 11 additions & 0 deletions compat_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import babel from 'rollup-plugin-babel';
import babelConfig from './babel_config';

export default {
output: {
format: 'umd'
},
plugins: [
babel(babelConfig),
],
};
150 changes: 150 additions & 0 deletions eslint_src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module"
},
"env": {
"es6": true
},
"rules": {
"valid-typeof": 2,
"no-console": 1,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-extra-semi": 1,
"no-func-assign": 2,
"func-call-spacing": [
2,
"never"
],
"no-unreachable": 1,
"no-unexpected-multiline": 2,
"no-sparse-arrays": 2,
"complexity": [
1,
18
],
"dot-location": [
2,
"property"
],
"no-implied-eval": 2,
"no-loop-func": 2,
"no-magic-numbers": [
1,
{
"ignore": [
-1,
0,
1,
2
]
}
],
"no-useless-call": 2,
"no-useless-concat": 2,
"no-delete-var": 2,
"no-shadow": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unused-vars": 1,
"consistent-return": 1,
"curly": [
2,
"multi-line"
],
"eqeqeq": 2,
"no-extend-native": 2,
"no-global-assign": 2,
"no-extra-bind": 2,
"no-redeclare": 2,
"array-bracket-spacing": 2,
"brace-style": [
1,
"1tbs"
],
"no-mixed-spaces-and-tabs": 2,
"no-tabs": 2,
"prefer-arrow-callback": 1,
"prefer-const": 2,
"prefer-template": 2,
"prefer-spread": 2,
"quotes": [
2,
"double",
{
"avoidEscape": true
}
],
"max-depth": [
2,
6
],
"max-len": [
2,
80
],
"no-nested-ternary": 2,
"no-unneeded-ternary": 2,
"strict": [
2,
"global"
],
"indent": [
2,
4,
{
"SwitchCase": 1
}
],
"no-useless-escape": 2,
"no-duplicate-imports": 2,
"no-unsafe-negation": 2,
"no-use-before-define": [
2,
{
"functions": false,
"classes": false
}
],
"space-infix-ops": 2,
"no-constant-condition": [
1,
{
"checkLoops": false
}
],
"no-empty": 1,
"use-isnan": 2,
"dot-notation": 2,
"no-caller": 2,
"no-else-return": 2,
"no-eval": 2,
"no-implicit-globals": 2,
"no-iterator": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-octal": 2,
"no-proto": 2,
"no-sequences": 2,
"block-spacing": 2,
"eol-last": 2,
"key-spacing": 2,
"no-trailing-spaces": 2,
"prefer-rest-params": 2,
"no-cond-assign": 2,
"keyword-spacing": 2,
"arrow-parens": [
2,
"as-needed"
],
"no-useless-return": 2,
"semi": 2,
"spaced-comment": [
2,
"always"
]
}
}
22 changes: 22 additions & 0 deletions eslint_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"plugins": [
"mocha"
],
"rules": {
"mocha/no-exclusive-tests": "error",
"mocha/no-identical-title": "error"
}
}
9 changes: 9 additions & 0 deletions mocha_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

require("@babel/polyfill");
require("@babel/register")({
plugins: [
"@babel/plugin-proposal-async-generator-functions",
"@babel/plugin-transform-modules-commonjs"
]
});

0 comments on commit 05684dc

Please sign in to comment.