array-group-str-omit-num-char2.1.48

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])

In other words, that variable you imported, "groupStr" (or "brambles" or whatever) is a function which consumes two input arguments.

§ API - Input

Input argumentTypeObligatory?Description
sourceArrayArrayyesArray of zero or more strings
otpsPlain objectnoAn 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 keyTypeDefaultDescription
wildcardString*What to use to mark grouped characters
dedupePleaseBooleantrueBy 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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 arrayiffy-if-string 3.11.38
Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else.
📦 array-includes-with-glob 2.12.42
like _.includes but with wildcards
📦 array-pull-all-with-glob 4.12.72
pullAllWithGlob - like _.pullAll but pulling stronger, with globs
📦 array-of-arrays-sort-by-col 2.12.13
sort array of arrays by column, rippling the sorting outwards from that column
📦 array-of-arrays-into-ast 1.9.50
turns an array of arrays of data into a nested tree of plain objects