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-do-while/__snapshots__/loop-do-while.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[`loopDoWhile function is a pure function 1`] = `
Array [
1,
2,
3,
4,
5,
]
`;
1 change: 1 addition & 0 deletions src/loop-do-while/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './loop-do-while';
21 changes: 21 additions & 0 deletions src/loop-do-while/loop-do-while.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { loopDoWhile } from './loop-do-while';

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

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

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

test('2 |> loopDoWhile <10, x => x² === 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.

Wrong title for this test case

expect(loopDoWhile<number>(x => x < 10, x => x ** 2)(10)).toBe(100);
});
});

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-do-while/loop-do-while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @module TBD_A=>TBD_B
*/
/**
* This method loop on an input:
* - at first, output = iteratee(input)
* - while predicat(output) is true, output = iteratee(output)
* - finally, returns output
* @param predicate The function apply on each loop to decide to :
* continue (return true), or to stop (return false)
* @param iteratee The iteratee invoked on each loop.
* @return the function to apply on the input
* @example
* ```
* loopDoWhile<number>(x => x < 10, x => x ** 2)(10) // 100
* ```
* @example Using the chain
* ```
* chain(10)
* .chain(loopDoWhile<number>(x => x < 10, x => x ** 2))
* .value() // 100
* ```
*/
export function loopDoWhile<T>(
predicate: (element: T) => boolean,
iteratee: (element: T) => T
): (input: T) => T {
return (input: T) => {
let output = input;
do {
output = iteratee(output);
} while (predicate(output));

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