Skip to content

Commit

Permalink
allow for custom default value in series prev/next functions (#230)
Browse files Browse the repository at this point in the history
* allow for default value in series prev/next

* bump version -> 10.6.0
sodiray authored Jan 8, 2023
1 parent f1f48c0 commit eb864e5
Showing 6 changed files with 38 additions and 17 deletions.
13 changes: 8 additions & 5 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
@@ -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,
13 changes: 8 additions & 5 deletions cdn/radash.js
Original file line number Diff line number Diff line change
@@ -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,
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",
17 changes: 12 additions & 5 deletions src/series.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
8 changes: 8 additions & 0 deletions src/tests/series.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
@@ -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', () => {

1 comment on commit eb864e5

@vercel
Copy link

@vercel vercel bot commented on eb864e5 Jan 8, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.