-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arrays): add arraySeq(), arrayIterator() & tests
- Loading branch information
1 parent
541e9c8
commit d94df57
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Nullable } from "@thi.ng/api"; | ||
|
||
/** | ||
* Returns iterator of nullable array w/ optional index range support | ||
* and/or reverse iteration order. The default range covers the entire | ||
* array. | ||
* | ||
* @remarks | ||
* If `start` > `end`, yields values in reverse order. No bounds | ||
* checking is performed. | ||
* | ||
* @param buf - array or null | ||
* @param start - start index | ||
* @param end - end index (excluded) | ||
*/ | ||
export function* arrayIterator<T>( | ||
buf: Nullable<ArrayLike<T>>, | ||
start = 0, | ||
end?: number | ||
) { | ||
if (!buf) return; | ||
start = start; | ||
end === undefined && (end = buf.length); | ||
const step = start <= end ? 1 : -1; | ||
for (; start !== end; start += step) { | ||
yield buf[start]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ISeq, Nullable } from "@thi.ng/api"; | ||
|
||
/** | ||
* Returns a zero-copy {@link @thi.ng/api#ISeq} for the given array and | ||
* optionally defined index range. | ||
* | ||
* @remarks | ||
* See {@link arrayIterator} for related functionality. | ||
* | ||
* @param buf - array | ||
* @param start - start index | ||
* @param end - end index (excluded) | ||
*/ | ||
export const arraySeq = <T>( | ||
buf: Nullable<ArrayLike<T>>, | ||
start = 0, | ||
end?: number | ||
): ISeq<T> => ({ | ||
first() { | ||
if (!buf) return; | ||
end === undefined && (end = buf.length); | ||
return start < end! ? buf[start] : undefined; | ||
}, | ||
next() { | ||
if (!buf) return; | ||
end === undefined && (end = buf.length); | ||
const i = start + 1; | ||
return i < end! ? arraySeq<T>(buf, i, end) : undefined; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as assert from "assert"; | ||
import { arrayIterator } from "../src"; | ||
|
||
describe("arrayIterator", () => { | ||
it("basics", () => { | ||
assert.deepEqual([...arrayIterator(null)], []); | ||
assert.deepEqual([...arrayIterator([])], []); | ||
assert.deepEqual([...arrayIterator([1])], [1]); | ||
assert.deepEqual([...arrayIterator([1, 2, 3, 4], 2)], [3, 4]); | ||
assert.deepEqual([...arrayIterator([1, 2, 3, 4], 2, 3)], [3]); | ||
assert.deepEqual([...arrayIterator([1, 2, 3, 4], 3, -1)], [4, 3, 2, 1]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as assert from "assert"; | ||
import { arraySeq } from "../src"; | ||
|
||
describe("arraySeq", () => { | ||
it("basics", () => { | ||
assert.notEqual(arraySeq(null), undefined); | ||
assert.equal(arraySeq(null).first(), undefined); | ||
assert.equal(arraySeq(null).next(), undefined); | ||
assert.equal(arraySeq([]).first(), undefined); | ||
assert.equal(arraySeq([]).next(), undefined); | ||
assert.equal(arraySeq([1]).first(), 1); | ||
assert.equal(arraySeq([1]).next(), undefined); | ||
assert.equal(arraySeq([1, 2]).first(), 1); | ||
// prettier-ignore | ||
assert.equal(arraySeq([1, 2]).next()!.first(), 2); | ||
// prettier-ignore | ||
assert.equal(arraySeq([1, 2]).next()!.next(), undefined); | ||
// prettier-ignore | ||
assert.equal(arraySeq([1, 2, 3]).next()!.next()!.first(), 3); | ||
}); | ||
}); |