Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: An alias method to output readonly array for Seq #1

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/leseq/src/operators/async/chunkAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { AsyncGen, AsyncOperator, AsyncSeq } from '../../asyncSeq';
* @typeParam T Source element type.
* @category Async Operators
*/
export const chunkAsync = <T>(size: number): AsyncOperator<T, T[]> =>
async function* chunkAsync(source: AsyncSeq<T>): AsyncGen<T[]> {
export const chunkAsync = <T>(size: number): AsyncOperator<T, readonly T[]> =>
async function* chunkAsync(source: AsyncSeq<T>): AsyncGen<readonly T[]> {
let ch: T[] = [];
for await (const one of source) {
ch.push(one);
Expand Down
4 changes: 2 additions & 2 deletions packages/leseq/src/operators/async/groupByAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export const groupByAsync = <T, TComparableValue, TKey = T, TValue = T>(
keySelector: (target: T) => Promise<TKey>,
elementSelector: (target: T) => Promise<TValue> = asyncDefaultSelector,
comparableValueForKey?: (key: TKey) => Promise<TComparableValue>
): AsyncOperator<T, { key: TKey; values: TValue[] }> =>
async function* groupByAsync(source: AsyncSeq<T>): AsyncGen<{ key: TKey; values: TValue[] }> {
): AsyncOperator<T, { key: TKey; values: readonly TValue[] }> =>
async function* groupByAsync(source: AsyncSeq<T>): AsyncGen<{ key: TKey; values: readonly TValue[] }> {
const resultMap = new Map<TComparableValue | TKey, { key: TKey; values: TValue[] }>();
const createKeyValue = async (i: T) => (comparableValueForKey ? comparableValueForKey(await keySelector(i)) : keySelector(i));

Expand Down
4 changes: 2 additions & 2 deletions packages/leseq/src/operators/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { Gen, Operator, Seq } from '../seq';
* @typeParam T Source element type.
* @category Operators
*/
export const chunk = <T>(size: number): Operator<T, T[]> =>
function* chunk(source: Seq<T>): Gen<T[]> {
export const chunk = <T>(size: number): Operator<T, readonly T[]> =>
function* chunk(source: Seq<T>): Gen<readonly T[]> {
let ch: T[] = [];
for (const one of source) {
ch.push(one);
Expand Down
4 changes: 2 additions & 2 deletions packages/leseq/src/operators/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const groupBy = <T, TComparableValue, TKey = T, TValue = T>(
keySelector: (target: T) => TKey,
elementSelector: (target: T) => TValue = defaultSelector,
comparableValueForKey?: (key: TKey) => TComparableValue
): Operator<T, { key: TKey; values: TValue[] }> =>
function* groupBy(source: Seq<T>): Gen<{ key: TKey; values: TValue[] }> {
): Operator<T, { key: TKey; values: readonly TValue[] }> =>
function* groupBy(source: Seq<T>): Gen<{ key: TKey; values: readonly TValue[] }> {
const resultMap = new Map<TComparableValue | TKey, { key: TKey; values: TValue[] }>();
const createKeyValue = (i: T) => (comparableValueForKey ? comparableValueForKey(keySelector(i)) : keySelector(i));
for (const one of source) {
Expand Down
2 changes: 1 addition & 1 deletion packages/leseq/src/operators/orderBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const orderBy = <T, TKey>(
compareFunction: (a: TKey, b: TKey) => number = defaultCompareFunction
): Operator<T> =>
function* orderBy(source: Seq<T>): Gen<T> {
const sortedArray = source.toArray().sort(createSortFunction(keySelector, sortType, compareFunction));
const sortedArray = source.toMutableArray().sort(createSortFunction(keySelector, sortType, compareFunction));
yield* sortedArray;
};

Expand Down
6 changes: 5 additions & 1 deletion packages/leseq/src/seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export class Seq<T> implements Iterable<T> {
}
}

toArray(): T[] {
toMutableArray(): T[] {
return [...this];
}

toArray(): readonly T[] {
return [...this];
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/leseq/tests/operators.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { concat, concatValue, skip, skipWhile, filter, flatten, from, map, orderBy, take, takeWhile, tap, uniq, groupBy, chunk, scan, union, difference, intersect, reverse } from '../src';

test('sec generates readonly array', () => {
const readOnlyOutput = from([1, 2, 3, 4]).toArray();

// @ts-expect-error Property 'push' does not exist on type 'readonly number[]'.
readOnlyOutput.push(99);

expect(readOnlyOutput).toEqual([1, 2, 3, 4, 99]);
});

test('operator: simple concat', () => {
const output = from([1, 2, 3, 4, 5])
.pipe(concat([6, 7]))
Expand Down Expand Up @@ -100,6 +109,17 @@ test('operator: map index', () => {
expect(indexes).toEqual([0, 1, 2]);
});

test('operator: orderBy is not mutate operation', () => {
const source = [6, 3, 2, 1, 4, 5];
const sourceCopy = source.slice();

const output = from(source)
.pipe(orderBy(i => i))
.toArray();
expect(output).toEqual([1, 2, 3, 4, 5, 6]);
expect(source).toEqual(sourceCopy);
});

test('operator: orderBy asc', () => {
const output = from([6, 3, 2, 1, 4, 5])
.pipe(orderBy(i => i))
Expand Down