Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/loop-until/__snapshots__/loop-until.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loopUntil function is a pure function 1`] = `
Array [
1,
2,
3,
4,
5,
]
`;
1 change: 1 addition & 0 deletions src/loop-until/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './loop-until';
17 changes: 17 additions & 0 deletions src/loop-until/loop-until.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { loopUntil } from './loop-until';

describe('loopUntil function', () => {
const input = [1, 2, 3, 4, 5];

test('is a pure function', () => {
loopUntil<ReadonlyArray<number>>(
arr => 10 < arr.length,
arr => [...arr, 0]
)(input);
expect(input).toMatchSnapshot();
});

test('2 |> loopUntil 10<, x => x² === 16', () => {
expect(loopUntil<number>(x => 10 < x, x => x ** 2)(2)).toBe(16);
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a test case with an array as input

});
36 changes: 36 additions & 0 deletions src/loop-until/loop-until.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @module any=>any
*/
/**
* This method loop on an input:
* - at first, output = input
* - while predicat(output) is false, output = iteratee(output)
* - finally, returns output
* @param predicate The function apply on each loop to decide to :
* continue (return false), or to stop (return true)
* @param iteratee The iteratee invoked on each loop.
* @return the function to apply on the input
* @example
* ```
* loopWhile<number>(x => 10 < x, x => x ** 2)(2) // 16
* ```
* @example Using the chain
* ```
* chain(2)
* .chain(loopWhile<number>(x => 10 < x, x => x ** 2))
* .value() // 16
* ```
*/
export function loopUntil<T>(
predicate: (element: T) => boolean,
iteratee: (element: T) => T
): (input: T) => T {
return (input: T) => {
let output = input;
while (!predicate(output)) {
output = iteratee(output);
}

return output;
};
}
1 change: 1 addition & 0 deletions src/taninsam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './keys';
export * from './last';
export * from './length';
export * from './loop-for';
export * from './loop-until';
export * from './map';
export * from './max';
export * from './max-by';
Expand Down