1.0.0
is :: * -> Boolean
(any)
boolean
:
(Array)
Boolean
:
isArray(1); //=> false
isArray([1,2,3]); //=> true
isNot :: * -> Boolean
(any)
boolean
:
(any)
boolean
:
(string)
boolean
:
Note that the counts are not sorted and appear in the order that the values
of the input array xs
is traveresd.
Object
:
Where each key is a unique string and each key value is the
occurrence frequency of string. Sorting by frequency is not performed.
const xs = ["apple", "orange", "banana", "apple", "banana", "apple"];
countOccurrences(xs); //=> { apple: 3, orange: 1, banana: 2 }
Takes a key
and returns a function that expects an array of objects
(containing a property equal to key
) that finally returns an Object<K,V>
where K
is the key name used to group all elements of xs
and V
is an
array of elements from xs
matching the key
value.
(String)
function (xs: Array<T>): Object<K, V>
:
Where
K
is the key name used to group all the elements of
xs
.
const persons = [
{ id: 1, name: "John" },
{ id: 2, name: "James" },
{ id: 3, name: "Jack" },
{ id: 4, name: "James" },
{ id: 5, name: "Jack" },
{ id: 6, name: "James" },
];
dictGroupByKey ("name") (persons); //=> {John: Array[1], James: Array[3], Jack: Array[2]}
//
// {
// "John": [
// { "id": 1, "name": "John" }
// ],
// "James": [
// { "id": 2, "name": "James" },
// { "id": 4, "name": "James" },
// { "id": 6, "name": "James" }
// ],
// "Jack": [
// { "id": 3, "name": "Jack" },
// { "id": 5, "name": "Jack" }
// ]
// }
//
Takes a key
, returns a function that expects an array of objects, finally
returns an object of unique keys.
(String)
function (xs: Array<T>): Object<T<K>, Boolean<True>>
:
Where
K
is the key value on some T and
Boolean<True>
is placeholder.
const persons = [
{ id: 1, name: "John" },
{ id: 2, name: "James" },
{ id: 3, name: "Jack" },
{ id: 4, name: "James" },
{ id: 5, name: "Jack" },
{ id: 6, name: "James" },
];
dictByKey ("name") (persons); //=> { John: true, James: true, Jack: true }
(function)
function (xs: array): array
:
Removes elements of the array that evaluate to false when coerced to Boolean.
filterFalse :: [*] -> [*]
([])
[]
:
function (obj: Object): Object
:
const obj = {apple: 3, banana: 2, orange: 1};
sliceObject({start: 0, end: 1})(obj); //=> {apple: 3}
sliceObject({start: 0, end: 2})(obj); //=> {apple: 3, banana: 2}
sliceObject({start: 0, end: 3})(obj); //=> {apple: 3, banana: 2, orange: 1}
sliceObject({start: 0, end: 9})(obj); //=> {apple: 3, banana: 2, orange: 1}
(Object)
Object
:
const occurrenceCounts = { apple: 3, orange: 1, banana: 2 };
sortByCountDesc(occurrenceCounts); //=> {apple: 3, banana: 2, orange: 1}
(String)
function (xs: Array<T>): Array<String>
:
const persons = [
{ id: 1, name: "John" },
{ id: 2, name: "James" },
{ id: 3, name: "Jack" },
{ id: 4, name: "James" },
{ id: 5, name: "Jack" },
{ id: 6, name: "James" },
];
uniqValuesByKey ("name") (persons); //=> ["John", "James", "Jack"]
(Number)
{hours: number, minutes: number, seconds: number}
:
toTimeFromSeconds(501227); //=> { hours: 139, minutes: 13, seconds: 46 }
(Date)
(number | NaN)
:
(Date)
string
:
(string)
string
:
Sunday Monday ... Friday Saturday
(string)
string
:
1 2 ... 30 31
(string)
string
:
00 01 ... 22 23
(string)
string
:
January February ... November December
getCurrYear :: () -> Number
number
:
(string)
string
:
1970 1971 ... 2029 2030
Date
:
The date one month ago from today.
(any
= false
)
string
:
getISO8601DateWithTimeOffsetFromUTC(); //=> "2007-04-05T12:30-02:00"
getISO8601DateWithTimeOffsetFromUTC(true); //=> "2020-01-23T14:33:15.4260000-05:00"
getPrevYear :: () -> Number
number
:
(string)
A string represntation of some date.
boolean
:
new Date(undefined); //=> Invalid Date
isNaN(new Date(undefined)); //=> true
isNaN(new Date(undefined)) === false ; //=> false
isValidDate("2020-01-01"); //=> true
isValidDate(undefined); //=> false
A wrapper function over date-fns
that converts errors arising from
unparseable dates into a boolean.
(Date)
boolean
:
(String)
function (date: any): String
:
The formatted date string of an empty
string if the date was not valid.
Attempts to parse a date a date and return the formatted version with given style.
(string)
(string)
string
:
An empty string if the date cannot be parsed otherwise the
formatted date.
string
:
The value of
fallbackText
if the date cannot be parsed
otherwise the formatted date.
// ex. 1
tryFormatDate(
"2016-01-01 13:41:51.000",
"EEE MMM dd, yyyy 'at' HH:mm",
""
); //=> "Fri Jan 01, 2016 at 13:41"
// ex. 2
tryFormatDate(
"",
"EEE MMM dd, yyyy 'at' HH:mm",
"n/a"
); //=> "n/a"
// ex. 3
const payload = { id: 123, createdAt: "2016-01-01 13:41:51.000" };
const createdAtParsed = payload.createdAt && tryFormatDate(
payload.createdAt,
"EEE MMM dd, yyyy 'at' HH:mm",
""
); //=> "Fri Jan 01, 2016 at 13:41"
(Array<T>)
(T | undefined)
:
const xs = ["a", "b", "c"];
first([]); //=> undefined
first(xs); //=> "a"
delete xs[0];
first(xs); //=> "b"
delete xs[1];
first(xs); //=> "c""
delete xs[2];
first(xs); //=> undefined
(Array<T>)
function (xs: Array<T>): Array<T>
:
(Array<T>)
An array of primitive-laden arrays.
Array<T>
:
The arrays merged into one.
flatten2d([[1,2,3], [4,5,6]]); //=> [1,2,3,4,5,6]
head :: [a] -> [a]
(Array<T>)
Array<T>
:
(any)
function (array): boolean
:
makeRangesBetween([0]); //=> [[0,0]]
makeRangesBetween([3,21,41,136]); //=> [[0,3], [3,21], [21,41], [41,136]]
makeRangesBetween([0,-22,3,5,-7,136]); //=> [[0,-22], [-22,3], [3,5], [5,-7], [-7,136]]
Extracts those objects having the exact value on one or more of the specified keys.
Extracts those objects having the exact value on every single specified key.
(array)
array
:
tail :: [a] -> a
(Array<any>)
Array<any>
:
(array)
A 1d array of primitives.
Array<any>
:
unique([]); //=> []
unique([1,1,2,3]); //=> [1,2,3]
unique(["a","b","b","c"]); //=> ["a","b","c"]
Adds two numbers together.
Number
:
The sum of adding two numbers.
(string)
(function (x: Object): any | undefined)
:
v of <k,v> or undefined
(object)
boolean
:
(Object)
Boolean
:
(object)
Array<string>
:
(String)
function (target: String): String
:
append ("Hello ") ("world"); //=> "Hello world"
(String)
String
:
(string)
(any)
Array<string>
:
const str = "The quick brown fox jumps over the lazy dog";
const actual = chunkStringByIndices(str, [[0,3],[3,6]]);
const expected = [
{ segment: ["The", "quick", "brown"], keyword: false },
{ segment: ["fox"], keyword: true },
{ segment: ["jumps", "over"], keyword: false }
];
const actual2 = chunkStringByIndices(str, [[0,3],[3,6],[6,9]]);
const expected2 = [
{ segment: ["The", "quick", "brown"], keyword: false },
{ segment: ["fox"], keyword: true },
{ segment: ["jumps", "over"], keyword: false },
{ segment: ["the"], keyword: true },
{ segment: ["lazy", "dog"], keyword: false }
];
Breaks apart a string where keywords are found.
Array<Object<string, Array<TextFragment>>>
:
const text = "The quick brown dog jumped over the fox, but why did the dog not bark?";
const actual = chunkStringByKeywords(text, ["brown", "dog"]);
const expected = [
{ segment: ["The", "quick"], keyword: false },
{ segment: ["brown"], keyword: true },
{ segment: [], keyword: false },
{ segment: ["dog"], keyword: true },
{
segment: ["jumped", "over", "the", "fox,", "but", "why", "did", "the"],
keyword: false
},
{ segment: ["dog"], keyword: true },
{ segment: ["not", "bark?"], keyword: false }
];
Splits the string into an array and finds the indices where the keyword is found using a wildcard search on the end of the keyword.
Array<number>
:
An array containing the array indices (if any) of where
each keyword was after splitting the string into words.
findKeyword("The quick brown fox jumps over the lazy dog", "the"); //=> [0,6]
Given a string, will perform a wildcard search on the exploded string for each supplied keyword and return an array of index positions found for each keyword.
Searching is case insensitive.
Object<string, Array<number>>
:
An object containing the keyword and an
array of numbers representing the index positions of the supplied string
exploded on whitespace.
const s = "The quick brown fox jumps over the lazy dog";
findKeywords(s, ["the","dog"]); //=> { "the": [0,6], "dog": [8] }
Object<string, Array<TextFragment>>
:
const text = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?";
const actual = getBufferedTextFragmentsAroundKeywords(
text,
["pleasure", "great", "responsible"],
3
);
const expected = {
pleasure: [
[
{ segment: ["idea", "of", "denouncing"], keyword: false },
{ segment: ["pleasure"], keyword: true },
{ segment: ["and", "praising", "pain"], keyword: false }
],
[
{ segment: ["dislikes,", "or", "avoids"], keyword: false },
{ segment: ["pleasure"], keyword: true },
{ segment: ["itself,", "because", "it"], keyword: false }
],
[
{ segment: ["because", "it", "is"], keyword: false },
{ segment: ["pleasure,"], keyword: true },
{ segment: ["but", "because", "those"], keyword: false }
],
[
{ segment: ["how", "to", "pursue"], keyword: false },
{ segment: ["pleasure"], keyword: true },
{ segment: ["rationally", "encounter", "consequences"], keyword: false }
],
[
{ segment: ["him", "some", "great"], keyword: false },
{ segment: ["pleasure."], keyword: true },
{ segment: ["To", "take", "a"], keyword: false }
],
[
{ segment: ["to", "enjoy", "a"], keyword: false },
{ segment: ["pleasure"], keyword: true },
{ segment: ["that", "has", "no"], keyword: false }
],
[
{ segment: ["produces", "no", "resultant"], keyword: false },
{ segment: ["pleasure?"], keyword: true },
{ segment: [], keyword: false }
]
],
great: [
[
{ segment: ["teachings", "of", "the"], keyword: false },
{ segment: ["great"], keyword: true },
{ segment: ["explorer", "of", "the"], keyword: false }
],
[
{ segment: ["procure", "him", "some"], keyword: false },
{ segment: ["great"], keyword: true },
{ segment: ["pleasure.", "To", "take"], keyword: false }
]
],
responsible: []
};
Retrieves a string fragment padded with words around some word positioned at index.
(string)
The targeted string.
(number)
The array position identifying the desired word once
the string has been split on whitespace.
string
:
const words = "hello world how are you doing today";
getSurroundingWords(words, 0, 0); //=> "hello"
getSurroundingWords(words, 0, 1); //=> "hello world"
getSurroundingWords(words, 2, 1); //=> "world how are"
getSurroundingWords(words, 2, 2); //=> "hello world how are you"
getSurroundingWords(words, 2, 3); //=> "hello world how are you doing"
getSurroundingWords(words, 2, 4); //=> "hello world how are you doing today"
(string)
(number)
The amount of words around a keyword to return
for all instances of keyword matches.
Object<string, Array<string>>
:
For each keyword found in the source
string, returns a text fragment with a word buffer starting from the
keyword position.
const source = "The quick brown fox jumps over the lazy dog but, why did the dog not bark at the fox.";
const textSurroundingKeywords = getSurroundingWordsAroundKeywords(source, ["dog","lazy"], 2);
const output = {
dog: [
"the lazy dog but, why",
"did the dog not bark"
],
lazy: [
"over the lazy dog but,"
]
};
String
:
String
:
String
:
(String)
String
:
(boolean)
function (word: string): string
:
maybePluralize(xs.length > 1)("result"); //=> "results"
`${[].length} ${maybePluralize([].length > 1)("result")} found.`; //=> "0 result found"
`${["a"].length} ${maybePluralize(["a"].length > 1)("result")} found.`; //=> "1 result found"
`${["a","b"].length} ${maybePluralize(["a","b"].length > 1)("result")} found.`; //=> "2 results found"
(string)
string
:
(String)
String
:
removeCommas("hello,world"); //=> "helloworld"
removeCommas("hello, world"); //=> "hello world"
removeCommas("a,b,c"); //=> abc
removeNonDigits :: String -> String
(string)
string
:
removeNonDigits ("foo123bar"); //=> 123
Replaces all occurrences of commas with a single space ensuring to truncate two or more resulting adjacent spaces to a single space.
In practice, this means that a comma at the beginning or the end of a string will be replaced with a whitespace but will not be trimmed.
replaceCommaWithWhitespace :: String -> String
Type: (any | Array<any>)
replaceCommaWithWhitespace("hello,,, world"); //=> "hello world"
replaceCommaWithWhitespace(",hello, world"); //=> " hello world"
replaceCommaWithWhitespace("hello, world,"); //=> "hello world "
replaceCommaWithWhitespace(",hello,world,"); //=> " hello world "
(String)
String
:
replaceNonNumericChars("a1b2c3"); //=> "123"
(String)
String
:
replaceNonNumericChars("a1-b2-c3"); //=> "1-2-3"
A curried wrapper around the native string.replace
function. Function is
extracted from the sanctuary.js docs.
Does not truncate whitespace.
replaceString :: (String -> (String -> String)) -> String
replaceString ("bar") ("") ("foo bar baz"); //=> "foo bar" (note the double space)
replaceString (/a/gi) ("") ("foo bar baz"); //=> "foo br bz"
(String)
Array<String>
:
splitAt :: Number -> [A] -> [A]
(number)
function (array): [[], []]
:
splitAt (2) (["a","b","c","d","e"]); //=> [["a","b"],["c","d","e"]]
Returns array of substrings resulting from splitting the original string wherever a comma was found.
splitComma :: String -> [String]
String
:
splitComma ("foo,bar,baz"); //=> ["foo", "bar", "baz"]
splitComma ("foo, bar, baz"); //=> ["foo", " bar", " baz"]
splitComma (""); //=> [""]
Returns array of substrings resulting from splitting the original string wherever a comma was found.
splitComma :: String -> [String]
String
:
splitCommaRegex ("foo,bar,baz"); //=> ["foo", "bar", "baz"]
splitCommaRegex ("foo, bar, baz"); //=> ["foo", " bar", " baz"]
splitCommaRegex (""); //=> [""]
Breaks apart a string into an array of substrings wherever newline
chars are found - these are \r\n
and \n
.
splitComma :: String -> [String]
String
:
splitNewLines ("hello \n world"); //=> ["hello ", " world"]
splitNewLines ("hello \r\n world"); //=> ["hello ", " world"]
splitNewLines ("hello \n \n \n world"); //=> ["hello ", " ", " ", " world"]
splitNewLines (""); //=> [""]
splitOn :: string -> string -> string[]
(string)
function (x: string): Array<string>
:
splits(";")("hello;world"); //=> ["hello", "world"]
splits(";")("hello;world;"); //=> ["hello", "world", ""]
Breaks apart a string into its array of substrings wherever the ASCII 32
SPACE
character is found.
splitSpace :: String -> [String]
String
:
splitSpace ("foo bar baz"); //=> ["foo", "bar", "baz"]
splitSpace ("hello world"); //=> ["hello", "", "", "world"]
splitSpace (""); //=> [""]
Breaks apart a string into its array of substrings wherever the ASCII 32
SPACE
character is found.
splitSpaceRegex :: String -> [String]
String
:
splitSpaceRegex ("foo bar baz"); //=> ["foo", "bar", "baz"]
splitSpaceRegex ("hello world"); //=> ["hello", "", "", "world"]
splitSpaceRegex (""); //=> [""]
Breaks apart a string into its array of substrings wherever whitespace characters are found.
This corresponds to all whitespace characters represented by the \s
metacharacter in regex.
splitWhitespace :: String -> [String]
String
:
splitWhitespace ("hello \t world"); //=> "hello", "", "", "world"
(string)
string
:
(String)
String
:
Truncates contiguous ASCII 32 SPACE
chars into one.
truncateSpace :: String -> String
String
:
truncateSpace ("hello world"); //=> "hello world"
Truncates all whitespace chars to a single space char.
truncateWhitespace :: String -> String
String
:
truncateWhitespace ("hello world"); //=> "hello world"
truncateWhitespace ("foo \t \v bar"); //=> "foo bar"
(String)
String
:
(string)
string
:
(string)
string
:
(Object)
An object of <k,v> items
function (k: string): string
:
Function expecting a key to
lookup in the dict.
const dict = { GL: "Gold", SI: "Silver" };
define (dict) ("GL"); //=> "Gold"
define (dict) ("CL"); //=> "CL"
(Number)
Promise<undefined>
:
Note that decimal figures are dropped off.
(number)
string
:
formatNumberWithComma(100000); //=> "100,000"
(string)
string
:
string
:
makeTimeSpent(0, 0); //=> ""
makeTimeSpent(1, 0); //=> "1 hr"
makeTimeSpent(7, 0); //=> "7 hrs"
makeTimeSpent(0, 1); //=> "1 min"
makeTimeSpent(0, 7); //=> "7 mins"
makeTimeSpent(1, 1); //=> "1 hr, 1 min"
makeTimeSpent(1, 7); //=> "1 hr, 7 mins"
makeTimeSpent(7, 1); //=> "7 hrs, 1 min"
Array<Number>
:
const parsedFloats = parseFloats(["-79.8620667737253", "43.52457849715662"]);
console.log(parsedFloats); //=> [-79.8620667737253, 43.52457849715662]
(any)
String
:
prototype({}); //=> "[object Object]"
prototype([]); //=> "[object Array]"
prototype(7); //=> "[object Number]"
prototype(""); //=> "[object String]"
prototype(true); //=> "[object Boolean]"
prototype(false); //=> "[object Boolean]"
prototype(null); //=> "[object Null]"
prototype(undefined); //=> "[object Undefined]"
prototype(void 0); //=> "[object Undefined]"
prototype(); //=> "[object Undefined]"
(string)
string
:
(string)
string
:
Returns a promise that resolves to an object containing the base64 and mimetype values of a passed in file.
(File)
Promise<{base64: string, mimeType: string}>
:
Returns a promise that reads a binary file as a dataUrl (containing a mimetype and a base64 string).
(File)
Promise
:
showEither :: String -> String -> String
(string)
function (x: string): string
:
The
fallback
will always be returned
when an empty string or non-string values are supplied for
x
.
// ex. 1
showEither("n/a")("blue"); //=> "blue"
showEither("n/a")(" "); //=> " "
showEither("n/a")(""); //=> "n/a"
showEither("n/a")(null); //=> "n/a"
showEither("n/a")(false); //=> "n/a"
showEither("n/a")(0); //=> "n/a"
// ex. 2
const personName = "John";
showEither("n/a")(!!personName ? `Hello ${personName}` : null); //=> "Hello John"
// ex. 3
showEither("n/a")(appendText(" kg")(toSignificantFigures(0)(80.55))); //=> "80 kg"
(any)
function (target: String): String
:
False when the target cannot be
parsed, otherwise the string representation of the number rounded to
significant figures.
toSignificantFigures (2) ("abc"); //=> false
toSignificantFigures (2) (100.999); //=> "100.99"
toString :: * -> String
(any)
string
: