fix(striker-ui-api): add onEach callback to join

main
Tsu-ba-me 2 years ago
parent 107b119d52
commit 3cb7c0f7ee
  1. 15
      striker-ui-api/src/lib/join.ts
  2. 1
      striker-ui-api/src/types/JoinOptions.d.ts

@ -2,13 +2,24 @@ import call from './call';
const join = (
elements: string[] | string | undefined,
{ beforeReturn, elementWrapper = '', separator = '' }: JoinOptions = {},
{
beforeReturn,
elementWrapper = '',
onEach = (element: string) => element,
separator = '',
}: JoinOptions = {},
) => {
const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`;
const toReturn =
elements instanceof Array && elements.length > 0
? `${elementWrapper}${elements.join(joinSeparator)}${elementWrapper}`
? `${elementWrapper}${elements
.slice(1)
.reduce<string>(
(previous, element) =>
`${previous}${joinSeparator}${onEach(element)}`,
elements[0],
)}${elementWrapper}`
: undefined;
return call<string | undefined>(beforeReturn, {

@ -1,5 +1,6 @@
type JoinOptions = {
beforeReturn?: (toReturn?: string) => string;
elementWrapper?: string;
onEach?: (element: string) => string;
separator?: string;
};

Loading…
Cancel
Save