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

allow for custom default value in series prev/next functions #230

Merged
merged 3 commits into from
Jan 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,21 @@ const series = (items, toKey = (item) => `${item}`) => {
const last = () => {
return itemsByIndex[items.length - 1];
};
const next = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? first();
const next = (current, defaultValue) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? defaultValue ?? first();
};
const previous = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? last();
const previous = (current, defaultValue) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? defaultValue ?? last();
};
const spin = (current, num) => {
if (num === 0)
return current;
const abs = Math.abs(num);
const rel = abs > items.length ? abs % items.length : abs;
return list(0, rel - 1).reduce(num > 0 ? next : previous, current);
return list(0, rel - 1).reduce(
(acc) => num > 0 ? next(acc) : previous(acc),
current
);
};
return {
min,
Expand Down
13 changes: 8 additions & 5 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,18 +764,21 @@ var radash = (function (exports) {
const last = () => {
return itemsByIndex[items.length - 1];
};
const next = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? first();
const next = (current, defaultValue) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? defaultValue ?? first();
};
const previous = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? last();
const previous = (current, defaultValue) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? defaultValue ?? last();
};
const spin = (current, num) => {
if (num === 0)
return current;
const abs = Math.abs(num);
const rel = abs > items.length ? abs % items.length : abs;
return list(0, rel - 1).reduce(num > 0 ? next : previous, current);
return list(0, rel - 1).reduce(
(acc) => num > 0 ? next(acc) : previous(acc),
current
);
};
return {
min,
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "10.5.0",
"version": "10.6.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
17 changes: 12 additions & 5 deletions src/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ export const series = <T>(
* in the series or default if the given value is
* the last item in the series
*/
const next = (current: T): T => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? first()
const next = (current: T, defaultValue?: T): T => {
return (
itemsByIndex[indexesByKey[toKey(current)] + 1] ?? defaultValue ?? first()
)
}
/**
* Given an item in the series returns the previous item
* in the series or default if the given value is
* the first item in the series
*/
const previous = (current: T): T => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? last()
const previous = (current: T, defaultValue?: T): T => {
return (
itemsByIndex[indexesByKey[toKey(current)] - 1] ?? defaultValue ?? last()
)
}
/**
* A more dynamic method than next and previous that
Expand All @@ -76,7 +80,10 @@ export const series = <T>(
if (num === 0) return current
const abs = Math.abs(num)
const rel = abs > items.length ? abs % items.length : abs
return list(0, rel - 1).reduce(num > 0 ? next : previous, current)
return list(0, rel - 1).reduce(
acc => (num > 0 ? next(acc) : previous(acc)),
current
)
}
return {
min,
Expand Down
8 changes: 8 additions & 0 deletions src/tests/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ describe('series module', () => {
const result = sut.next('friday')
assert.equal(result, 'monday')
})
test('returns the given default when the last is exhausted', () => {
const result = sut.next('friday', 'wednesday')
assert.equal(result, 'wednesday')
})
})

describe('previous function', () => {
Expand All @@ -67,6 +71,10 @@ describe('series module', () => {
const result = sut.previous('monday')
assert.equal(result, 'friday')
})
test('returns the given default when the first is exhausted', () => {
const result = sut.previous('monday', 'wednesday')
assert.equal(result, 'wednesday')
})
})

describe('spin function', () => {
Expand Down