@transformation/core
Advanced tools
Comparing version 4.1.1 to 4.1.2
{ | ||
"name": "@transformation/core", | ||
"version": "4.1.1", | ||
"version": "4.1.2", | ||
"description": "Create transformation pipelines", | ||
@@ -21,7 +21,8 @@ "main": "./src/index.js", | ||
"dependencies": { | ||
"medium": "1.1.1", | ||
"quick-lru": "^5.1.0" | ||
"lru": "^3.1.0", | ||
"medium": "1.1.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "9.0.3", | ||
"sinon": "^11.1.2", | ||
"unexpected": "12.0.3", | ||
@@ -33,3 +34,3 @@ "unexpected-steps": "^4.1.0" | ||
}, | ||
"gitHead": "38cac774e2c1a352cdfbf6fd30d4c7effb319025" | ||
"gitHead": "6b0749ca6bee0bf4100c98a38ceaddd5615b6da6" | ||
} |
@@ -396,10 +396,7 @@ # @transformation/core | ||
await expect(emitAll(asyncIterable(), [3, 4, 5]), "to yield items", [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]); | ||
await expect( | ||
emitAll(asyncIterable(), [3, 4, 5]), | ||
"to yield items", | ||
[0, 1, 2, 3, 4, 5] | ||
); | ||
``` | ||
@@ -1019,3 +1016,3 @@ | ||
pipeline( | ||
emitItems(0, 1, 2, 0, 1, 2, 0, 1, 2), | ||
emitItems(0, 1, 2, 2, 1, 0, 0, 1, 2), | ||
memorize( | ||
@@ -1027,3 +1024,3 @@ map((v) => `${v}: ${i++}`), | ||
"to yield items", | ||
["0: 0", "1: 1", "2: 2", "0: 0", "1: 3", "2: 2", "0: 4", "1: 3", "2: 5"] | ||
["0: 0", "1: 1", "2: 2", "2: 2", "1: 1", "0: 3", "0: 3", "1: 1", "2: 4"] | ||
); | ||
@@ -1215,2 +1212,47 @@ ``` | ||
## retry | ||
Retries a series steps according to the given retry options. | ||
Options | ||
- max (default 5) the maximum number of retries | ||
- delay (default 100) the delay basis for the retry | ||
- strategy (default exponential) can also be linear | ||
The code snippet below will fetch todos with ids from 0 to 5. It a request | ||
fails it will retry at most 5 times where the delay of 100 is doubled for each | ||
retry. | ||
```js | ||
const { retry } = require("@transformation/core"); | ||
``` | ||
```js | ||
await program( | ||
emitItems(0, 1, 2, 3, 4, 5), | ||
map(n => `https://jsonplaceholder.typicode.com/todos/${0}`), | ||
retry( | ||
map(url => fetch(url)) | ||
map(res => res.json()) | ||
), | ||
tap() | ||
) | ||
``` | ||
You can override the defaults the following way: | ||
```js | ||
await program( | ||
emitItems(0, 1, 2, 3, 4, 5), | ||
map(n => `https://jsonplaceholder.typicode.com/todos/${0}`), | ||
retry( | ||
{ max: 3, delay: 300, strategy: 'linear' }, | ||
map(url => fetch(url)) | ||
map(res => res.json()) | ||
), | ||
tap() | ||
) | ||
``` | ||
## reverse | ||
@@ -1217,0 +1259,0 @@ |
@@ -7,10 +7,7 @@ const expect = require("unexpected").clone().use(require("unexpected-steps")); | ||
it("emits all items from an array", async () => { | ||
await expect(emitAll([0, 1, 2, 3, 4, 5]), "to yield items", [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]); | ||
await expect( | ||
emitAll([0, 1, 2, 3, 4, 5]), | ||
"to yield items", | ||
[0, 1, 2, 3, 4, 5] | ||
); | ||
}); | ||
@@ -36,10 +33,7 @@ | ||
await expect(emitAll(asyncIterator()), "to yield items", [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]); | ||
await expect( | ||
emitAll(asyncIterator()), | ||
"to yield items", | ||
[0, 1, 2, 3, 4, 5] | ||
); | ||
}); | ||
@@ -55,11 +49,8 @@ | ||
await expect(emitAll(asyncIterable(), [3, 4, 5]), "to yield items", [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]); | ||
await expect( | ||
emitAll(asyncIterable(), [3, 4, 5]), | ||
"to yield items", | ||
[0, 1, 2, 3, 4, 5] | ||
); | ||
}); | ||
}); |
@@ -6,11 +6,8 @@ const expect = require("unexpected").clone().use(require("unexpected-steps")); | ||
it("emits the given items on the output channel", async () => { | ||
await expect(emitItems(0, 1, 2, 3, 4, 5), "to yield items", [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]); | ||
await expect( | ||
emitItems(0, 1, 2, 3, 4, 5), | ||
"to yield items", | ||
[0, 1, 2, 3, 4, 5] | ||
); | ||
}); | ||
}); |
@@ -6,11 +6,13 @@ const pipeline = require("./pipeline"); | ||
const forEach = require("./forEach"); | ||
const QuickLRU = require("quick-lru"); | ||
const LRU = require("lru"); | ||
const memorize = (step, { maxSize = 1000, key = (v) => v } = {}) => { | ||
const memorize = (step, { maxSize = Infinity, key = (v) => v } = {}) => { | ||
const keySelector = typeof key === "string" ? (value) => value[key] : key; | ||
const lru = new QuickLRU({ maxSize }); | ||
const lru = new LRU(maxSize); | ||
const has = (value) => typeof lru.peek(keySelector(value)) !== "undefined"; | ||
return pipeline( | ||
chose((value) => (lru.has(keySelector(value)) ? "cached" : "notCached"), { | ||
chose((value) => (has(value) ? "cached" : "notCached"), { | ||
cached: map((value) => lru.get(keySelector(value))), | ||
@@ -17,0 +19,0 @@ notCached: pipeline( |
@@ -37,3 +37,3 @@ const expect = require("unexpected").clone().use(require("unexpected-steps")); | ||
describe("when given a max size", () => { | ||
it("defaults to an infinite cache", async () => { | ||
it("the memorize only stores the max number of elements", async () => { | ||
let i = 0; | ||
@@ -43,3 +43,3 @@ | ||
pipeline( | ||
emitItems(0, 1, 2, 0, 1, 2, 0, 1, 2), | ||
emitItems(0, 1, 2, 2, 1, 0, 0, 1, 2), | ||
memorize( | ||
@@ -51,3 +51,3 @@ map((v) => `${v}: ${i++}`), | ||
"to yield items", | ||
["0: 0", "1: 1", "2: 2", "0: 0", "1: 3", "2: 2", "0: 4", "1: 3", "2: 5"] | ||
["0: 0", "1: 1", "2: 2", "2: 2", "1: 1", "0: 3", "0: 3", "1: 1", "2: 4"] | ||
); | ||
@@ -54,0 +54,0 @@ }); |
@@ -16,12 +16,14 @@ const step = require("./step"); | ||
const start = partitionNumber * size; | ||
const end = start + size - 1; | ||
partitionNumber++; | ||
if (nextBatch.length > 0) { | ||
const start = partitionNumber * size; | ||
const end = start + size - 1; | ||
partitionNumber++; | ||
await put( | ||
Group.create({ | ||
key: `[${start};${end}]`, | ||
items: nextBatch, | ||
}) | ||
); | ||
await put( | ||
Group.create({ | ||
key: `[${start};${end}]`, | ||
items: nextBatch, | ||
}) | ||
); | ||
} | ||
@@ -28,0 +30,0 @@ if (value === CLOSED) break; |
@@ -20,2 +20,27 @@ const expect = require("unexpected").clone().use(require("unexpected-steps")); | ||
}); | ||
it("handles the case where a partiion isn't full", async () => { | ||
await expect( | ||
pipeline(emitItems(0, 1, 2, 3, 4, 5, 6), partition(2)), | ||
"to yield items", | ||
[ | ||
Group.create({ key: "[0;1]", items: [0, 1] }), | ||
Group.create({ key: "[2;3]", items: [2, 3] }), | ||
Group.create({ key: "[4;5]", items: [4, 5] }), | ||
Group.create({ key: "[6;7]", items: [6] }), | ||
] | ||
); | ||
}); | ||
it("handles the case where all partiions is full", async () => { | ||
await expect( | ||
pipeline(emitItems(0, 1, 2, 3, 4, 5), partition(2)), | ||
"to yield items", | ||
[ | ||
Group.create({ key: "[0;1]", items: [0, 1] }), | ||
Group.create({ key: "[2;3]", items: [2, 3] }), | ||
Group.create({ key: "[4;5]", items: [4, 5] }), | ||
] | ||
); | ||
}); | ||
}); |
148753
117
3685
2010
4
+ Addedlru@^3.1.0
+ Addedinherits@2.0.4(transitive)
+ Addedlru@3.1.0(transitive)
- Removedquick-lru@^5.1.0
- Removedquick-lru@5.1.1(transitive)