Skip to content

Commit

Permalink
feat@insert.all
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyan Totev committed Jan 25, 2024
1 parent 98ae0bc commit f23f54e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
22 changes: 22 additions & 0 deletions NEXT_VERSION_CHECKLIST.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- insert

- insertAll
- into
- invert
Expand All @@ -8,6 +9,8 @@

run immutable script

---
transform this mocha test to jest(also remove describe block as I dont need it)
---
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
---
Expand Down Expand Up @@ -152,6 +155,25 @@ publish after march 2024

export function anyPass<T, U extends T[]>(predicates: { [K in keyof U]: (x: T) => x is U[K]; }): (input: T) => input is U[number];
===
export default function deepqual(foo, bar) {
var ctor, len;
if (foo === bar) return true;
if (foo && bar && (ctor=foo.constructor) === bar.constructor) {
if (ctor === Date) return foo.getTime() === bar.getTime();
if (ctor === RegExp) return foo.toString() === bar.toString();
if (ctor === Array && (len=foo.length) === bar.length) {
while (len-- && dequal(foo[len], bar[len]));
return len === -1;
}
if (ctor === Object) {
if (Object.keys(foo).length !== Object.keys(bar).length) return false;
for (len in foo) if (!(len in bar) || !dequal(foo[len], bar[len])) return false;
return true;
}
}
return foo !== foo && bar !== bar;
}
===
from bookmarks:

https://arethetypeswrong.github.io/?p=ramda%400.29.1
Expand Down
24 changes: 23 additions & 1 deletion files/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5984,7 +5984,29 @@ Notes:
*/
// @SINGLE_MARKER
export function insert<T>(x: T): T;
export function insert(index: number): <T>(itemToInsert: T, list: T[]) => T[];
export function insert<T>(index: number, itemToInsert: T): (list: T[]) => T[];
export function insert<T>(index: number, itemToInsert: T, list: T[]): T[];

/*
Method: insertAll
Explanation:
Example:
```
```
Categories:
Notes:
*/
// @SINGLE_MARKER
export function insertAll(index: number): <T>(itemsToInsert: T[], list: T[]) => T[];
export function insertAll<T>(index: number, itemsToInsert: T[]): (list: T[]) => T[];
export function insertAll<T>(index: number, itemsToInsert: T[], list: T[]): T[];

// RAMBDAX_MARKER_START

Expand Down
38 changes: 18 additions & 20 deletions source/insert.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { insert } from './insert';

describe('insert', () => {
it('inserts an element into the given list', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(2, 'x', list)).toEqual(['a', 'b', 'x', 'c', 'd', 'e']);
});
it('inserts an element into the given list', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(2, 'x', list)).toEqual(['a', 'b', 'x', 'c', 'd', 'e']);
});

it('inserts another list as an element', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(2, ['s', 't'], list)).toEqual([
'a',
'b',
['s', 't'],
'c',
'd',
'e',
]);
});
it('inserts another list as an element', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(2, ['s', 't'], list)).toEqual([
'a',
'b',
['s', 't'],
'c',
'd',
'e',
]);
});

it('appends to the end of the list if the index is too large', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(8, 'z', list)).toEqual(['a', 'b', 'c', 'd', 'e', 'z']);
});
it('appends to the end of the list if the index is too large', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insert(8, 'z', list)).toEqual(['a', 'b', 'c', 'd', 'e', 'z']);
});
7 changes: 7 additions & 0 deletions source/insertAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { curry } from './curry';

export function insertAllFn(index, listToInsert, list) {
return [...list.slice(0, index), ...listToInsert, ...list.slice(index)];
}

export const insertAll = curry(insertAllFn);
29 changes: 29 additions & 0 deletions source/insertAll.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { insertAll } from './insertAll';

it('inserts a list of elements into the given list', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insertAll(2, ['x', 'y', 'z'], list)).toEqual([
'a',
'b',
'x',
'y',
'z',
'c',
'd',
'e',
]);
});

it('appends to the end of the list if the index is too large', () => {
const list = ['a', 'b', 'c', 'd', 'e'];
expect(insertAll(8, ['p', 'q', 'r'], list)).toEqual([
'a',
'b',
'c',
'd',
'e',
'p',
'q',
'r',
]);
});

0 comments on commit f23f54e

Please sign in to comment.