Skip to content

Commit

Permalink
Make series support non-primitive values (#149)
Browse files Browse the repository at this point in the history
* make series support non-primitive values
  • Loading branch information
sodiray authored Dec 11, 2022
1 parent b902e0a commit 5f19016
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 114 deletions.
62 changes: 38 additions & 24 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,42 +703,56 @@ const uid = (length, specials = "") => {
);
};

const series = (...items) => {
const { itemsByValue, itemsByIndex } = items.reduce(
const series = (items, toKey = (item) => `${item}`) => {
const { indexesByKey, itemsByIndex } = items.reduce(
(acc, item, idx) => ({
itemsByValue: {
...acc.itemsByValue,
[item]: idx
indexesByKey: {
...acc.indexesByKey,
[toKey(item)]: idx
},
itemsByIndex: {
...acc.itemsByIndex,
[idx]: item
}
}),
{
itemsByValue: {},
indexesByKey: {},
itemsByIndex: {}
}
);
const min = (a, b) => {
return indexesByKey[toKey(a)] < indexesByKey[toKey(b)] ? a : b;
};
const max = (a, b) => {
return indexesByKey[toKey(a)] > indexesByKey[toKey(b)] ? a : b;
};
const first = () => {
return itemsByIndex[0];
};
const last = () => {
return itemsByIndex[items.length - 1];
};
const next = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? first();
};
const previous = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? 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 {
min: (a, b) => {
return itemsByValue[a] < itemsByValue[b] ? a : b;
},
max: (a, b) => {
return itemsByValue[a] > itemsByValue[b] ? a : b;
},
first: () => {
return itemsByIndex[0];
},
last: () => {
return itemsByIndex[items.length - 1];
},
next: (current, defaultValue) => {
return itemsByIndex[itemsByValue[current] + 1] ?? defaultValue;
},
previous: (current, defaultValue) => {
return itemsByIndex[itemsByValue[current] - 1] ?? defaultValue;
}
min,
max,
first,
last,
next,
previous,
spin
};
};

Expand Down
62 changes: 38 additions & 24 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,42 +706,56 @@ var radash = (function (exports) {
);
};

const series = (...items) => {
const { itemsByValue, itemsByIndex } = items.reduce(
const series = (items, toKey = (item) => `${item}`) => {
const { indexesByKey, itemsByIndex } = items.reduce(
(acc, item, idx) => ({
itemsByValue: {
...acc.itemsByValue,
[item]: idx
indexesByKey: {
...acc.indexesByKey,
[toKey(item)]: idx
},
itemsByIndex: {
...acc.itemsByIndex,
[idx]: item
}
}),
{
itemsByValue: {},
indexesByKey: {},
itemsByIndex: {}
}
);
const min = (a, b) => {
return indexesByKey[toKey(a)] < indexesByKey[toKey(b)] ? a : b;
};
const max = (a, b) => {
return indexesByKey[toKey(a)] > indexesByKey[toKey(b)] ? a : b;
};
const first = () => {
return itemsByIndex[0];
};
const last = () => {
return itemsByIndex[items.length - 1];
};
const next = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] + 1] ?? first();
};
const previous = (current) => {
return itemsByIndex[indexesByKey[toKey(current)] - 1] ?? 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 {
min: (a, b) => {
return itemsByValue[a] < itemsByValue[b] ? a : b;
},
max: (a, b) => {
return itemsByValue[a] > itemsByValue[b] ? a : b;
},
first: () => {
return itemsByIndex[0];
},
last: () => {
return itemsByIndex[items.length - 1];
},
next: (current, defaultValue) => {
return itemsByIndex[itemsByValue[current] + 1] ?? defaultValue;
},
previous: (current, defaultValue) => {
return itemsByIndex[itemsByValue[current] - 1] ?? defaultValue;
}
min,
max,
first,
last,
next,
previous,
spin
};
};

Expand Down
Loading

0 comments on commit 5f19016

Please sign in to comment.