blob: 38830cca628a0ae0d6e08765c3504918554fe0c6 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001'use strict';
2
3/**
4 * @license
5 * Copyright 2016 Leif Olsen. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * A javascript utility for conditionally creating a list of strings.
22 * The function takes any number of arguments which can be a string or object.
23 * Inspired by (but not copied from) JedWatson/classnames, https://github.com/JedWatson/classnames
24 *
25 * @param {*} args the strings and/or objects to
26 * @return {Array} a list of strings
27 * @example
28 * // Returns ['foo', 'bar', 'baz', 'quux']
29 * stringList(', ', 'foo', { bar: true, duck: false }, 'baz', { quux: true });
30 * @example see the tests for more examples
31 */
32const stringList = (...args) => {
33
34 const isString = str => str != null && typeof str === 'string';
35
36 const flatten = list => list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
37
38 const objectToStrings = arg =>
39 Object.keys(arg)
40 .filter(key => arg[key])
41 .map(key => key);
42
43 return args
44 .filter(arg => !!arg)
45 .map(arg => isString(arg) ? arg : objectToStrings(arg))
46 .reduce((result, arg) => result.concat(Array.isArray(arg) ? flatten(arg) : arg), []);
47};
48
49/**
50 * A simple javascript utility for conditionally joining strings together.
51 * The function takes a delimiter string and any number of arguments which can be a string or object.
52 *
53 * @param delimiter delimiter to separate joined strings
54 * @param {*} args the strings and/or objects to join
55 * @return {String} the joined strings
56 * @example
57 * // Returns 'foo, bar, baz, quux'
58 * joinStrings(', ', 'foo', { bar: true, duck: false }, 'baz', { quux: true });
59 * @example see the tests for more examples
60 */
61const joinStrings = (delimiter = ' ', ...args) => stringList(...args).join(delimiter);
62
63/**
64 * Generates a random string with a given length
65 * @param n {Integer} length of generated string
66 * @see http://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
67 * @return {String} the random string
68 * @example
69 * // Returns e.g. 'pd781w0y'
70 * randomString(8);
71 * @example see the tests for more examples
72 */
73const randomString = ( n=12 ) => Array( n+1 ).join((`${Math.random().toString(36)}00000000000000000`).slice(2, 18)).slice(0, n);
74
75export { joinStrings, randomString, stringList };
76