blob: ab0045a03e87c37ce4add5a2f4728d5fa145de6e [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 */
32
33Object.defineProperty(exports, "__esModule", {
34 value: true
35});
36exports.stringList = exports.randomString = exports.joinStrings = undefined;
37
38var _keys = require('babel-runtime/core-js/object/keys');
39
40var _keys2 = _interopRequireDefault(_keys);
41
42function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
44var stringList = function stringList() {
45 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
46 args[_key] = arguments[_key];
47 }
48
49 var isString = function isString(str) {
50 return str != null && typeof str === 'string';
51 };
52
53 var flatten = function flatten(list) {
54 return list.reduce(function (a, b) {
55 return a.concat(Array.isArray(b) ? flatten(b) : b);
56 }, []);
57 };
58
59 var objectToStrings = function objectToStrings(arg) {
60 return (0, _keys2.default)(arg).filter(function (key) {
61 return arg[key];
62 }).map(function (key) {
63 return key;
64 });
65 };
66
67 return args.filter(function (arg) {
68 return !!arg;
69 }).map(function (arg) {
70 return isString(arg) ? arg : objectToStrings(arg);
71 }).reduce(function (result, arg) {
72 return result.concat(Array.isArray(arg) ? flatten(arg) : arg);
73 }, []);
74};
75
76/**
77 * A simple javascript utility for conditionally joining strings together.
78 * The function takes a delimiter string and any number of arguments which can be a string or object.
79 *
80 * @param delimiter delimiter to separate joined strings
81 * @param {*} args the strings and/or objects to join
82 * @return {String} the joined strings
83 * @example
84 * // Returns 'foo, bar, baz, quux'
85 * joinStrings(', ', 'foo', { bar: true, duck: false }, 'baz', { quux: true });
86 * @example see the tests for more examples
87 */
88var joinStrings = function joinStrings() {
89 for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
90 args[_key2 - 1] = arguments[_key2];
91 }
92
93 var delimiter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ' ';
94 return stringList.apply(undefined, args).join(delimiter);
95};
96
97/**
98 * Generates a random string with a given length
99 * @param n {Integer} length of generated string
100 * @see http://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
101 * @return {String} the random string
102 * @example
103 * // Returns e.g. 'pd781w0y'
104 * randomString(8);
105 * @example see the tests for more examples
106 */
107var randomString = function randomString() {
108 var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12;
109 return Array(n + 1).join((Math.random().toString(36) + '00000000000000000').slice(2, 18)).slice(0, n);
110};
111
112exports.joinStrings = joinStrings;
113exports.randomString = randomString;
114exports.stringList = stringList;