@mll-lab/js-utils 2.7.0 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/array.d.ts CHANGED
@@ -23,3 +23,9 @@ export declare const EMPTY_ARRAY: never[];
23
23
  */
24
24
  export declare function insertIf<T>(condition: boolean, ...elements: Array<T>): Array<T>;
25
25
  export declare function last<T, A extends Array<T>>(array: A): A extends NonEmptyArray<T> ? T : T | undefined;
26
+ /**
27
+ * Appends element to array if not included or removes it.
28
+ *
29
+ * Never mutates the given array, always returns a new array.
30
+ */
31
+ export declare function toggleElement<T>(array: Array<T>, element: T): Array<T>;
@@ -9466,6 +9466,16 @@ function last(array) {
9466
9466
  // @ts-expect-error too magical
9467
9467
  return array[array.length - 1];
9468
9468
  }
9469
+ /**
9470
+ * Appends element to array if not included or removes it.
9471
+ *
9472
+ * Never mutates the given array, always returns a new array.
9473
+ */
9474
+ function toggleElement(array, element) {
9475
+ return array.includes(element)
9476
+ ? array.filter((e) => e !== element)
9477
+ : array.concat(element);
9478
+ }
9469
9479
 
9470
9480
  function toInteger(dirtyNumber) {
9471
9481
  if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
@@ -15201,6 +15211,26 @@ function includesIgnoreCase(needle, haystack) {
15201
15211
  function firstLine(multilineText) {
15202
15212
  return multilineText.split('\n', 1)[0];
15203
15213
  }
15214
+ function joinNonEmpty(maybeStrings, separator) {
15215
+ return maybeStrings.filter(Boolean).join(separator);
15216
+ }
15217
+
15218
+ /**
15219
+ * Converts pixel value to number.
15220
+ *
15221
+ * @example "4px" to 4
15222
+ */
15223
+ function pxToNumber(pixels) {
15224
+ const count = (pixels.match(/px/g) || []).length;
15225
+ if (count > 1 || !pixels.endsWith('px')) {
15226
+ throw new Error(`'${pixels}' does not contain a single 'px'`);
15227
+ }
15228
+ const parsedValue = parseFloat(pixels);
15229
+ if (Number.isNaN(parsedValue)) {
15230
+ throw new Error(`'${pixels}' is not a valid single pixel-value`);
15231
+ }
15232
+ return parsedValue;
15233
+ }
15204
15234
 
15205
15235
  exports.DOTLESS_DATE_FORMAT = DOTLESS_DATE_FORMAT;
15206
15236
  exports.EMAIL_REGEX = EMAIL_REGEX;
@@ -15237,6 +15267,7 @@ exports.isString = isString;
15237
15267
  exports.isToday = isToday;
15238
15268
  exports.isURL = isURL;
15239
15269
  exports.isWord = isWord;
15270
+ exports.joinNonEmpty = joinNonEmpty;
15240
15271
  exports.last = last;
15241
15272
  exports.parseDate = parseDate;
15242
15273
  exports.parseDotlessDate = parseDotlessDate;
@@ -15248,6 +15279,8 @@ exports.parseIsoDate = parseIsoDate;
15248
15279
  exports.parseIsoDateTime = parseIsoDateTime;
15249
15280
  exports.parseSecondlessDateTime = parseSecondlessDateTime;
15250
15281
  exports.pluralize = pluralize;
15282
+ exports.pxToNumber = pxToNumber;
15251
15283
  exports.round = round;
15284
+ exports.toggleElement = toggleElement;
15252
15285
  exports.withoutIndex = withoutIndex;
15253
15286
  //# sourceMappingURL=index.common.js.map