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 = ( const join = (
elements: string[] | string | undefined, elements: string[] | string | undefined,
{ beforeReturn, elementWrapper = '', separator = '' }: JoinOptions = {}, {
beforeReturn,
elementWrapper = '',
onEach = (element: string) => element,
separator = '',
}: JoinOptions = {},
) => { ) => {
const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`; const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`;
const toReturn = const toReturn =
elements instanceof Array && elements.length > 0 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; : undefined;
return call<string | undefined>(beforeReturn, { return call<string | undefined>(beforeReturn, {

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

Loading…
Cancel
Save