From f23f54e02b5117429e4708b8d69c18188b845e09 Mon Sep 17 00:00:00 2001 From: Deyan Totev Date: Thu, 25 Jan 2024 13:13:14 +0200 Subject: [PATCH] feat@insert.all --- NEXT_VERSION_CHECKLIST.md | 22 ++++++++++++++++++++++ files/index.d.ts | 24 +++++++++++++++++++++++- source/insert.spec.js | 38 ++++++++++++++++++-------------------- source/insertAll.js | 7 +++++++ source/insertAll.spec.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 source/insertAll.js create mode 100644 source/insertAll.spec.js diff --git a/NEXT_VERSION_CHECKLIST.md b/NEXT_VERSION_CHECKLIST.md index dc52dc7d..11506942 100644 --- a/NEXT_VERSION_CHECKLIST.md +++ b/NEXT_VERSION_CHECKLIST.md @@ -1,4 +1,5 @@ - insert + - insertAll - into - invert @@ -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 --- @@ -152,6 +155,25 @@ publish after march 2024 export function anyPass(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 diff --git a/files/index.d.ts b/files/index.d.ts index fc777c61..d627a938 100644 --- a/files/index.d.ts +++ b/files/index.d.ts @@ -5984,7 +5984,29 @@ Notes: */ // @SINGLE_MARKER -export function insert(x: T): T; +export function insert(index: number): (itemToInsert: T, list: T[]) => T[]; +export function insert(index: number, itemToInsert: T): (list: T[]) => T[]; +export function insert(index: number, itemToInsert: T, list: T[]): T[]; + +/* +Method: insertAll + +Explanation: + +Example: + +``` +``` + +Categories: + +Notes: + +*/ +// @SINGLE_MARKER +export function insertAll(index: number): (itemsToInsert: T[], list: T[]) => T[]; +export function insertAll(index: number, itemsToInsert: T[]): (list: T[]) => T[]; +export function insertAll(index: number, itemsToInsert: T[], list: T[]): T[]; // RAMBDAX_MARKER_START diff --git a/source/insert.spec.js b/source/insert.spec.js index 59213ee2..87f258fd 100644 --- a/source/insert.spec.js +++ b/source/insert.spec.js @@ -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']); }); diff --git a/source/insertAll.js b/source/insertAll.js new file mode 100644 index 00000000..5ba522a5 --- /dev/null +++ b/source/insertAll.js @@ -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); diff --git a/source/insertAll.spec.js b/source/insertAll.spec.js new file mode 100644 index 00000000..c15e8032 --- /dev/null +++ b/source/insertAll.spec.js @@ -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', + ]); +});