-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
54 lines (50 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Permutes the given term by prepending each prefix and appending each suffix.
*
* @param {string} term - The term to permute.
* @param {string[]} prefixes - The prefixes to prepend to the term.
* @param {string[]} suffixes - The suffixes to append to the term.
* @returns {string[]} - The permuted array of terms.
*/
export default function(term, prefixes = [], suffixes = []) {
const termWithPrefixes = prependEach(term, prefixes);
const termWithSuffixes = appendEach(term, suffixes);
const termWithPrefixesAndSuffixes = appendEach(termWithPrefixes, suffixes);
return [...termWithPrefixes, ...termWithSuffixes, ...termWithPrefixesAndSuffixes];
}
/**
* Prepends each element of the term array with each prefix in the prefixes array.
* @param {string | string[]} term - The term array.
* @param {string[]} prefixes - The prefixes array.
* @returns {string[]} - The resulting array with each element prepended with each prefix.
*/
export const prependEach = (term, prefixes) => {
let arr = coerceToArray(term);
return arr.map(t => prefixes.map(p => p + t)).flat();
}
/**
* Appends each suffix to every term in the array.
*
* @param {string | string[]} term - The term to append suffixes to.
* @param {string[]} suffixes - The array of suffixes to append.
* @returns {string[]} - The array of terms with suffixes appended.
*/
export const appendEach = (term, suffixes) => {
let arr = coerceToArray(term);
return arr.map(t => suffixes.map(s => t + s)).flat();
}
/**
* Coerces the given value to an array.
*
* @param {any} mysteryObject - The value to be coerced to an array.
* @returns {string[]} - The coerced array.
*/
export const coerceToArray = (mysteryObject) => {
if ( Array.isArray(mysteryObject) ) {
return mysteryObject;
}
if ( typeof mysteryObject === 'string' ) {
return [].concat(mysteryObject);
}
return undefined;
}