array-group-str-omit-num-char5.0.1

Groups array of strings by omitting number characters

Quick Take

import { strict as assert } from "assert";
import { groupStr } from "array-group-str-omit-num-char";

assert.deepEqual(
  groupStr(["a1-1", "a2-2", "b3-3", "c4-4"]),
  {
    "a*-*": 2,
    "b3-3": 1,
    "c4-4": 1,
  }
);

Idea

Take an array of strings, group those which differ only by a certain number.

For example, consider this array atomic CSS class names coming from some report (for example, email-comb output object's key deletedFromHead):

[
"wbr425-padding-top-1",
"wbr425-padding-top-2",
"main-title",
"wbr425-padding-top-3",
];

In real life, you could have for example, wbr425-padding-top-* would be shorter and go up to 500 and there were, let's say, 20 other groups like it.

This npm library groups strings, in this case producing:

{
"wbr425-padding-top-*": 3,
"main-title": 1
}

Notice the "425" in wbr425 was not replaced with wildcard because it was constant on all strings that were grouped. This feature, retaining constant digits, was the reason why we got into hassle producing this library.

You see, the quickest, alternative (gung-ho) algorithm is to replace all digits with "*" and filter the unique values, but "425" in wbr425 would be lost. That's why we need this library.

API

groupStr(sourceArray, [opts])

API - Input

Input argument Type Obligatory? Description
sourceArray Array yes Array of zero or more strings
otps Plain object no An Optional Options Object. See its API below.

By the way, none of the input arguments are mutated.

API - Output

An empty array input will give output of a empty plain object. A non-empty array (with at least one string inside) will yield a plain object: strings will be grouped and put as keys, they count will be put as integer values.

For example:

console.log(groupStr(["a1-1", "a2-2", "b3-3", "c4-4"]));
// {
// "a*-*": 2,
// "b3-3": 1,
// "c4-4": 1
// }

An Optional Options Object

Type: object - an Optional Options Object.

options object's key Type Default Description
wildcard String * What to use to mark grouped characters
dedupePlease Boolean true By default, input array's contents will be deduped. But that's at a cost of performance, so if you're 100% sure your strings will be unique, set it to false.

Here are all defaults in one place for copying:

{
wildcard: "*",
dedupePlease: true
}

To explicitly mark the refusal to set custom Optional Options, it can be also passed as a null or undefined value. In that case, defaults will be set.

Changelog

See it in the monorepo opens in a new tab, on GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 array-pull-all-with-glob 6.0.1
Like _.pullAll but with globs (wildcards)
📦 array-of-arrays-into-ast 3.0.1
Turns an array of arrays of data into a nested tree of plain objects
📦 array-includes-with-glob 4.0.1
Like _.includes but with wildcards
📦 arrayiffy-if-string 4.0.1
Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else
📦 array-of-arrays-sort-by-col 4.0.1
Sort array of arrays by column, rippling the sorting outwards from that column