Strings
Two flavors of every transform:
c.transform(value) // applied to each item (as a string)c.transform(value, key) // applied to each item's keyed valueAll return a new collection. The keyed form shallow-clones each plain-object item via cloneForSet before applying the change, so the source items are not modified. (Class instances and nested object references are passed through; the clone is shallow.)
Append / prepend / concat
c.appendString(s, key?) // item + sc.prependString(s, key?) // s + itemc.concatString(s, key?) // identical to appendString for stringscollect(["Ada", "Bob"]).appendString("!");// ["Ada!", "Bob!"]
collect([{ name: "Ada" }]).appendString("!", "name");// [{ name: "Ada!" }]Replace / remove
c.replaceString(search: string | RegExp, replacement: string, key?)c.replaceAllString(search: string, replacement: string, key?) // search is a string; promoted to `new RegExp(search, "g")`c.removeString(search: string | RegExp, key?) // replace with ""c.removeAllString(search: string, key?) // replace all with ""collect(["aba", "aaa"]).replaceString("a", "x");// ["xba", "xaa"]
collect(["aba", "aaa"]).replaceAllString("a", "x");// ["xbx", "xxx"]
collect(["aaba"]).removeAllString("a");// ["b"]
replaceAllStringALWAYS does a global regex replace (the first arg is forced intonew RegExp(s, "g")). If you pass a regex, that’s wrong — usereplaceString(/regex/g, ...)instead.
Trim
c.trim(value?: string = " ", key?: string)collect([" hi ", " x "]).trim(); // ["hi", "x"]collect(["##a##", "#b#"]).trim("#"); // ["a", "b"]Wraps reinforcements’ trim(string, chars), which strips repeated occurrences of chars from both ends.
Casting
c.string() // each item via String(item)c.number() // each item via Number(item)c.boolean() // each item via Boolean(item)collect([1, null, "x"]).string(); // ["1", "null", "x"]collect(["1", "abc", ""]).number(); // [1, NaN, 0]collect([0, 1, "", "x"]).boolean(); // [false, true, false, true]