barnard59-base 2.4.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # barnard59-base
2
2
 
3
+ ## 2.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 65983ad: Adds a variadic parameter to `map` and` filter` steps, which will be forwarded to their respecitve callback functions called for each chunk
8
+
9
+ ## 2.4.2
10
+
11
+ ### Patch Changes
12
+
13
+ - f6d593c: `limit` step would not break the pipeline when the stream produces a very large or infinite stream
14
+
3
15
  ## 2.4.1
4
16
 
5
17
  ### Patch Changes
package/filter.d.ts CHANGED
@@ -1,16 +1,19 @@
1
1
  export default filter;
2
2
  /**
3
- * <T>
3
+ * <T, A>
4
4
  */
5
- export type Filter<T> = (this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>;
5
+ export type Filter<T, A extends unknown[]> = (this: import('barnard59-core').Context, chunk: T, encoding: string, ...args: A) => boolean | Promise<boolean>;
6
6
  /**
7
- * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>} Filter<T>
7
+ * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string, ...args: A) => boolean | Promise<boolean>} Filter<T, A>
8
8
  * @template T
9
+ * @template {Array<unknown>} A
9
10
  */
10
11
  /**
11
12
  * @template T
13
+ * @template {Array<unknown>} A
12
14
  * @this {import('barnard59-core').Context}
13
- * @param {Filter<T>} func
15
+ * @param {Filter<T, A>} func
16
+ * @param {A} args
14
17
  * @return {import('stream').Transform}
15
18
  */
16
- declare function filter<T>(this: import("barnard59-core").Context, func: Filter<T>): import('stream').Transform;
19
+ declare function filter<T, A extends unknown[]>(this: import("barnard59-core").Context, func: Filter<T, A>, ...args: A): import('stream').Transform;
package/filter.js CHANGED
@@ -1,20 +1,23 @@
1
1
  import { obj } from 'through2'
2
2
 
3
3
  /**
4
- * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>} Filter<T>
4
+ * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string, ...args: A) => boolean | Promise<boolean>} Filter<T, A>
5
5
  * @template T
6
+ * @template {Array<unknown>} A
6
7
  */
7
8
 
8
9
  /**
9
10
  * @template T
11
+ * @template {Array<unknown>} A
10
12
  * @this {import('barnard59-core').Context}
11
- * @param {Filter<T>} func
13
+ * @param {Filter<T, A>} func
14
+ * @param {A} args
12
15
  * @return {import('stream').Transform}
13
16
  */
14
- function filter(func) {
17
+ function filter(func, ...args) {
15
18
  return obj((chunk, encoding, callback) => {
16
19
  Promise.resolve().then(() => {
17
- return func.call(this, chunk, encoding)
20
+ return func.call(this, chunk, encoding, ...args)
18
21
  }).then(result => {
19
22
  if (result) {
20
23
  return callback(null, chunk)
package/forEach.d.ts CHANGED
@@ -25,16 +25,16 @@ declare class ForEach extends stream.Duplex {
25
25
  * @param {(error?: (Error | null)) => void} callback
26
26
  */
27
27
  _write(chunk: any, encoding: string, callback: (error?: (Error | null)) => void): Promise<void>;
28
- createPipeline: (ptr: import("clownface").GraphPointer, arg?: Partial<import("barnard59-core").PipelineOptions> | undefined) => import("barnard59-core/lib/Pipeline").default;
28
+ createPipeline: (ptr: import("clownface").GraphPointer, arg?: Partial<import("barnard59-core").PipelineOptions> | undefined) => import("barnard59-core").Pipeline;
29
29
  ptr: import("clownface").GraphPointer;
30
30
  variable: string;
31
31
  variables: Map<string, unknown>;
32
32
  pull: ReadableToReadable.ReadFunction | null;
33
- set subPipeline(subPipeline: import("barnard59-core/lib/Pipeline").default);
33
+ set subPipeline(subPipeline: import("barnard59-core").Pipeline);
34
34
  /**
35
35
  * @type import('barnard59-core').Pipeline
36
36
  */
37
- get subPipeline(): import("barnard59-core/lib/Pipeline").default;
37
+ get subPipeline(): import("barnard59-core").Pipeline;
38
38
  }
39
39
  import stream from 'readable-stream';
40
40
  import ReadableToReadable from 'readable-to-readable';
package/limit.js CHANGED
@@ -12,6 +12,8 @@ export default function limit(limit) {
12
12
  count++
13
13
  if (count <= limit) {
14
14
  yield chunk
15
+ } else {
16
+ break
15
17
  }
16
18
  }
17
19
  }
package/map.d.ts CHANGED
@@ -1,29 +1,33 @@
1
1
  /**
2
- * @typedef {(this: import('barnard59-core').Context, chunk: From) => Promise<To> | To} MapCallback
2
+ * @typedef {(this: import('barnard59-core').Context, chunk: From, ...args: Args) => Promise<To> | To} MapCallback
3
3
  * @template From, To
4
+ * @template {Array<unknown>} Args
4
5
  */
5
6
  /**
6
7
  * @typedef {{
7
- * map: MapCallback<From, To>
8
+ * map: MapCallback<From, To, Args>
8
9
  * concurrency?: number
9
10
  * ordered?: boolean
10
11
  * objectMode?: boolean
11
- * }|MapCallback<From, To>} MapOptions
12
+ * }|MapCallback<From, To, Args>} MapOptions
12
13
  * @template From, To
14
+ * @template {Array<unknown>} Args
13
15
  */
14
16
  /**
15
17
  * Processes chunks with a transform function
16
18
  *
17
19
  * @this {import('barnard59-core').Context}
18
- * @param {MapOptions<From, To>} options Transform function or complex options
20
+ * @param {MapOptions<From, To, Args>} options Transform function or complex options
21
+ * @param {Args} args Additional arguments to pass to the transform function
19
22
  * @return {import('stream').Transform}
20
23
  * @template From, To
24
+ * @template {Array<unknown>} Args
21
25
  */
22
- export default function map<From, To>(this: import("barnard59-core").Context, options: MapOptions<From, To>): import('stream').Transform;
23
- export type MapCallback<From, To> = (this: import('barnard59-core').Context, chunk: From) => Promise<To> | To;
24
- export type MapOptions<From, To> = {
25
- map: MapCallback<From, To>;
26
+ export default function map<From, To, Args extends unknown[]>(this: import("barnard59-core").Context, options: MapOptions<From, To, Args>, ...args: Args): import('stream').Transform;
27
+ export type MapCallback<From, To, Args extends unknown[]> = (this: import('barnard59-core').Context, chunk: From, ...args: Args) => Promise<To> | To;
28
+ export type MapOptions<From, To, Args extends unknown[]> = {
29
+ map: MapCallback<From, To, Args>;
26
30
  concurrency?: number;
27
31
  ordered?: boolean;
28
32
  objectMode?: boolean;
29
- } | MapCallback<From, To>;
33
+ } | MapCallback<From, To, Args>;
package/map.js CHANGED
@@ -1,31 +1,35 @@
1
1
  import transform from 'parallel-transform'
2
2
 
3
3
  /**
4
- * @typedef {(this: import('barnard59-core').Context, chunk: From) => Promise<To> | To} MapCallback
4
+ * @typedef {(this: import('barnard59-core').Context, chunk: From, ...args: Args) => Promise<To> | To} MapCallback
5
5
  * @template From, To
6
+ * @template {Array<unknown>} Args
6
7
  */
7
8
 
8
9
  /**
9
10
  * @typedef {{
10
- * map: MapCallback<From, To>
11
+ * map: MapCallback<From, To, Args>
11
12
  * concurrency?: number
12
13
  * ordered?: boolean
13
14
  * objectMode?: boolean
14
- * }|MapCallback<From, To>} MapOptions
15
+ * }|MapCallback<From, To, Args>} MapOptions
15
16
  * @template From, To
17
+ * @template {Array<unknown>} Args
16
18
  */
17
19
 
18
20
  /**
19
21
  * Processes chunks with a transform function
20
22
  *
21
23
  * @this {import('barnard59-core').Context}
22
- * @param {MapOptions<From, To>} options Transform function or complex options
24
+ * @param {MapOptions<From, To, Args>} options Transform function or complex options
25
+ * @param {Args} args Additional arguments to pass to the transform function
23
26
  * @return {import('stream').Transform}
24
27
  * @template From, To
28
+ * @template {Array<unknown>} Args
25
29
  */
26
- export default function map(options) {
30
+ export default function map(options, ...args) {
27
31
  /**
28
- * @type {MapCallback<*, *>}
32
+ * @type {MapCallback<*, *, Args>}
29
33
  */
30
34
  let func
31
35
  let concurrency = 1
@@ -42,7 +46,7 @@ export default function map(options) {
42
46
 
43
47
  return transform(concurrency, { ordered, objectMode }, (data, callback) => {
44
48
  Promise.resolve().then(() => {
45
- return func.call(this, data)
49
+ return func.call(this, data, ...args)
46
50
  }).then(result => {
47
51
  callback(null, result)
48
52
  }).catch(callback)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barnard59-base",
3
- "version": "2.4.1",
3
+ "version": "2.5.0",
4
4
  "description": "Linked Data pipelines",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -51,6 +51,6 @@
51
51
  },
52
52
  "mocha": {
53
53
  "require": "../../test/mocha-setup.cjs",
54
- "loader": "tsm"
54
+ "loader": "ts-node/esm/transpile-only"
55
55
  }
56
56
  }
@@ -5,6 +5,7 @@ import filter from '../filter.js'
5
5
 
6
6
  describe('filter', () => {
7
7
  it('should pass pipeline context to callback function', async () => {
8
+ // given
8
9
  const input = new Readable({
9
10
  objectMode: true,
10
11
  read: () => {
@@ -17,11 +18,34 @@ describe('filter', () => {
17
18
  variable: new Map().set('condition', 'a'),
18
19
  }
19
20
 
21
+ // when
20
22
  const outStream = input.pipe(filter.call(context, function (chunk) {
21
23
  return this.variable.get('condition') === chunk
22
24
  }))
23
25
  const output = await array(outStream)
24
26
 
27
+ // then
25
28
  deepStrictEqual(output, ['a'])
26
29
  })
30
+
31
+ it('should pass additional arguments to callback function', async () => {
32
+ // given
33
+ const input = new Readable({
34
+ objectMode: true,
35
+ read: () => {
36
+ input.push(1)
37
+ input.push(2)
38
+ input.push(3)
39
+ input.push(null)
40
+ },
41
+ })
42
+
43
+ // when
44
+ const greaterThan = (chunk, _, minValue) => chunk > minValue
45
+ const outStream = input.pipe(filter.call(context, greaterThan, 2))
46
+ const output = await array(outStream)
47
+
48
+ // then
49
+ deepStrictEqual(output, [3])
50
+ })
27
51
  })
package/test/map.test.js CHANGED
@@ -25,6 +25,40 @@ describe('map', () => {
25
25
  deepStrictEqual(output, ['foo_a', 'foo_b'])
26
26
  })
27
27
 
28
+ it('should pass optional arguments to mapper function', async () => {
29
+ const input = new Readable({
30
+ read: () => {
31
+ input.push('a')
32
+ input.push('b')
33
+ input.push(null)
34
+ },
35
+ })
36
+
37
+ const prependPrefix = (chunk, prefix) => prefix + chunk
38
+ const outStream = input.pipe(map.call(context, prependPrefix, 'foo_'))
39
+ const output = await array(outStream)
40
+
41
+ deepStrictEqual(output, ['foo_a', 'foo_b'])
42
+ })
43
+
44
+ it('should pass optional arguments to mapper function when used with options object', async () => {
45
+ const input = new Readable({
46
+ read: () => {
47
+ input.push('a')
48
+ input.push('b')
49
+ input.push(null)
50
+ },
51
+ })
52
+
53
+ const prependPrefix = (chunk, prefix) => prefix + chunk
54
+ const outStream = input.pipe(map.call(context, {
55
+ map: prependPrefix,
56
+ }, 'foo_'))
57
+ const output = await array(outStream)
58
+
59
+ deepStrictEqual(output, ['foo_a', 'foo_b'])
60
+ })
61
+
28
62
  it('accepts a function as parameter', async () => {
29
63
  // given
30
64
  const transform = letter => letter.toUpperCase()