0.2.0 • Published 7 months ago

@ionutinit/custom-methods v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Custom Methods

A collection of methods I keep seeing myself using, so I decided to optimize them and put them in a package.

Offers TypeScript support.

npm i @ionutinit/custom-methods

 

capitalize

Capitalizes the first letter of a string or each element in an array of strings.

capitalize(string | string[]) => string | string[]

Optional parameters (string only)

When provided a string, an optional style parameter can be invoked to capitalize each word or sentence respectively.

capitalize(string, { style: "word" | "sentence" });

Examples

capitalize("hello");
// "Hello"
capitalize(["good", "day"]);
// ["Good". "Day"]
capitalize("hello. how are you?", { style: "word" });
// "Hello. How Are You?"
capitalize("hello. how are you?", { style: "sentence" });
// "Hello. How are you?"

 

concord

Creates grammatical concord (agreement) between a string and an associated number.

If the first argument is a string, it parses it into a number

concord(number | string, string) => string

Optional parameters

concord(
    number | string,
    pluralForm?: string,
    round?: {-1 | 1 | true | false}
)
  • pluralForm: provides the plural form of the string; useful for irregular nouns, otherwise defaults to string + s

  • round: rounds the number into an integer according to criteria provided; defaults to false

    • true: rounds up or down, according to decimal value

    • -1: rounds down

    • 1: rounds up

Examples

concord(0, "item");
// "no items"
concord(1, "item");
// "1 item"
concord("2", "item");
// "2 items"
concord(1, "leaf", "leaves");
// "1 leaf"
concord(3, "leaf", "leaves");
// "3 leaves"
concord("5.25", "item", { round: 1 });
// "6 items"
concord("5.95", "leaf", "leaves", { round: 1 });
// "5 leaves"

 

enumerate

Returns a natural-language enumeration of an array of strings.

enumerate(string[]) => string

Optional parameters

enumerate(
    string[],
    limit?: number,
    tail?: number,
    options?: {unique: boolean}
)
  • limit: sets the length of the had of the output enumeration, followed by "..."

  • tail: sets the length of the tail of the output enumeration; works only in conjunction with limit; it is ignored if is larger or equal than the limit or the remainder of the array

  • unique: only a list of the unique elements of the array is enumerated; defaults to false

Examples

enumerate(["one", "two"]);
// "one and two"
enumerate(["one", "two", "three"]);
// "one, two and three"
enumerate(["one", "two", "three", "four"]);
// "one, two, three and four"
enumerate(["one", "two", "three", "four"], { limit: 2 });
// "one, two..."
enumerate(["one", "one", "two", "two", "three", "three", "four", "four"], {
  limit: 2,
  unique: true,
});
// "one, two..."
enumerate(["one", "one", "two", "two", "three", "three", "four", "four"], {
  limit: 1,
  tail: 1,
  unique: true,
});
// "one... four"
enumerate(["one", "two", "three", "four"], { limit: 2, tail: 4 });
// "one, two..."

 

pipe

A simple pipeline function

pipeline( ...args: function) => any

Example

const increment = (x) => x + 1;
const double = (x) => x * 2;
const toString = (x) => x.toString();

const piped = pipe(increment, double, toString);
piped(2);
// "6"

 

reduceText

Returns a text reduced to the full word or sentence nearest to the limit provided. The limit refers to the length of the string.

The default characters for the limit are . ! ? .

reduceText(string, number) => string

Optional parameters

reduceText(
  string,
  number,
  params?: string,
)

params: provide customs characters to be considered as the limit; each individual character is taken into consideration separately

Examples

reduceText("Hello. How are you? I'm fine!", 14);
// "Hello. How are you?"
// The limits taken into consideration are 5 (.), 18 (?), 28 (!). Closest to the limit provided is "?", so the string is cut at that point
reduceText("Hello. How are you? I'm fine!", 7);
// "Hello."
reduceText("Hello. How are you? I'm fine!", 1, "?");
// "Hello. How are you?"
reduceText("Hello. How are you? I'm fine!", 1, " ");
// "Hello." -> if a blank space is provided as param, it returns the words up the limit provided
0.2.0

7 months ago

0.1.0

7 months ago