A collection of utility methods for number, string, object, and array manipulations.
- Version:
- 1.3.0
Methods
# static allNull(array) → {Boolean}
Checks if all elements in an array are null.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array
|
The array to check. |
- Since:
- 1.1.0
True if all elements in the array are null, False otherwise.
Boolean
Example
pastaparse.allNull([null, null, null]);
// => true
pastaparse.allNull([null, null, 1]);
// => false
# static chunk(array, sizeopt) → {Array}
Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array |
Array
|
The array to process. |
||
size |
Number
|
<optional> |
1 | The length of each chunk. |
- Since:
- 1.0.0
The new array of chunks.
Array
Example
pastaparse.chunk([1, 2, 3, 4], 2);
// => [[1, 2], [3, 4]]
pastaparse.chunk([1, 2, 3, 4, 5], 2);
// => [[1, 2], [3, 4], [5]]
# static clamp(number, lower, upper) → {Number}
Clamps number within the inclusive lower and upper bounds.
Parameters:
Name | Type | Description |
---|---|---|
number |
Number
|
The number to clamp. |
lower |
Number
|
The lower bound. |
upper |
Number
|
The upper bound. |
- Since:
- 1.0.0
The clamped number.
Number
Example
pastaparse.clamp(-10, -5, 5);
// => -5
pastaparse.clamp(10, -5, 5);
// => 5
pastaparse.clamp(2, 1, 3);
// => 2
# static countChar(string, character, caseSensitiveopt) → {Number}
Counts the amount of instances of a certain character found within a given string.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
String
|
The string to process. |
||
character |
String
|
The character to count. |
||
caseSensitive |
Boolean
|
<optional> |
true | If true, the search is case sensitive. True by default. |
- Since:
- 1.1.0
The number of 'character' instances found in 'string'.
Number
Example
pastaparse.countChar('Hello', 'l');
// => 2
pastaparse.countChar('Aardvark', 'a');
// => 2
pastaparse.countChar('Aardvark', 'a', false);
// => 3
# static drop(array, n) → {Array}
Creates a slice of array with n elements dropped from the beginning.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array
|
The array to process. |
n |
Number
|
The number of elements to drop. |
- Since:
- 1.0.0
The slice of array.
Array
Example
pastaparse.drop(["hi", "okay", "yes", "bye"], 2);
// => ["yes", "bye"]
pastaparse.drop(["hi", "bye"]);
// => ["bye"]
# static dropWhile(array, predicate) → {Array}
Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).
Parameters:
Name | Type | Description |
---|---|---|
array |
Array
|
The array to process. |
predicate |
function
|
The function invoked per iteration. |
- Since:
- 1.0.0
The slice of array.
Array
Example
const indexIsSmallerThanElement = (element, index) => index < element;
pastaparse.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement);
// => [0, 4]
# static equalizeArrayLengths(arrays) → {Array}
Determines the maximum length of the given arrays. Then, iterates through each array and appends the last item of the array until its length matches the maximum length.
Parameters:
Name | Type | Description |
---|---|---|
arrays |
Array
|
The nested array to equalize. |
- Since:
- 1.1.0
The equalized nested array containing the same amount of items.
Array
Example
pastaparse.equalizeArrayLengths([[], [1], []]);
// => [[null], [1], [null]]
pastaparse.equalizeArrayLengths([[1], [3, 2], [], [4, 5, 6]]);
// => [[1, 1, 1], [3, 2, 2], [null, null, null], [4, 5, 6]]
# static extractNumbers(string) → {Array.<Number>}
Extracts all the numbers from a given string. Useful for extracting numbers within a string as an array of numbers.
Parameters:
Name | Type | Description |
---|---|---|
string |
String
|
The string to extract numbers from. |
- Since:
- 1.1.0
The array of numbers extracted from the string.
Array.<Number>
Example
pastaparse.extractNumbers('3 little pigs ate 5 apples each making a total of 15 apples.');
// => [3, 5, 15]
pastaparse.extractNumbers('14.5 percent of the 20 people in class know that Pi is 3.14.');
// => [14.5, 20, 3.14]
pastaparse.extractNumbers('The price is $12.50.');
// => [12.5]
pastaparse.extractNumbers('The price is $12.50 and the quantity is 3.');
// => [12.5, 3]
# static feetInchesToFloat(feetInches) → {Number}
Converts a string of unit feet and inches to a float value in feet. Accepts the following formats: 2' - 6" ; 1' - 6 1/2" ; 0' 6" ; 0' 6 1/2"
Parameters:
Name | Type | Description |
---|---|---|
feetInches |
String
|
The string of feet and inches to convert. |
- Since:
- 1.1.0
If the input string is null or empty.
Error
The float value equivalent in feet.
Number
Example
pastaparse.feetInchesToFloat('2\' - 6"');
// => 2.5
pastaparse.feetInchesToFloat('1\' - 6 1/2"');
// => 1.5416666666666667
pastaparse.feetInchesToFloat('0\' 6"');
// => 0.5
pastaparse.feetInchesToFloat('0\' 6 1/2"');
// => 0.5416666666666666
# static findKey(object, predicate) → {*}
Finds the key of the first element matching the predicate instead of the element itself.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object
|
The object to inspect. |
predicate |
function
|
The function invoked per iteration. |
- Since:
- 1.0.0
The key of the matched element, else undefined.
*
Example
const startsWithV = string => string.startsWith('v');
pastaparse.findKey({ 'key': 'value' }, startsWithV);
// => 'key'
# static fractionToFloat(fraction) → {Number}
Attempts to convert a fraction string to a number.
Parameters:
Name | Type | Description |
---|---|---|
fraction |
String
|
The fraction string to convert. |
- Since:
- 1.1.0
If the input string is null or empty.
Error
The converted number.
Number
Example
pastaparse.fractionToFloat('1/2');
// => 0.5
pastaparse.fractionToFloat('1/4');
// => 0.25
pastaparse.fractionToFloat('5/8');
// => 0.625
pastaparse.fractionToFloat('3 3/4');
// => 3.75
# static has(object, key) → {Boolean}
Checks if key is a direct property of object.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object
|
The object to query. |
key |
String
|
The object property to check. |
- Since:
- 1.0.0
True if path exists, else false.
Boolean
Example
const object = { 'a': 1, 'b': 2, 'c': 3 };
pastaparse.has(object, 'a');
// => true
pastaparse.has(object, 'd');
// => false
# static inRange(number, startopt, end) → {Boolean}
Checks if n is between start and up to, but not including, end. If end is not specified, it's set to start with start then set to 0. If start is greater than end, the params are swapped to support negative ranges.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
number |
Number
|
The number to check. |
||
start |
Number
|
<optional> |
0 | The start of the range. |
end |
Number
|
The end of the range. |
- Since:
- 1.0.0
True if number is in range, else false.
Boolean
Example
pastaparse.inRange(2, 1, 3);
// => true
pastaparse.inRange(4, 8);
// => true
pastaparse.inRange(0, 1, 3);
// => false
pastaparse.inRange(4, 1, 3);
// => false
pastaparse.inRange(3, 4, 2);
// => true
pastaparse.inRange(1, 1, 3);
// => true
pastaparse.inRange(3, 1, 3);
// => false
# static invert(object) → {Object}
Creates an object composed of the inverted keys and values provided by object. If object contains duplicate values, subsequent values overwrite property assignments of previous values.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object
|
The object to invert. |
- Since:
- 1.0.0
The new inverted object.
Object
Example
let object = { 'a': 1, 'b': 2, 'c': 1 };
pastaparse.invert(object);
// => { '1': 'a', '2': 'b' }
let object = { 'a': 'apple', 'b': 'banana', 'c': 'cherry' };
pastaparse.invert(object);
// => { 'apple': 'a', 'banana': 'b', 'cherry': 'c' }
# static isNestedArray(array, strictlyNestedopt) → {Boolean}
Checks if an array is a nested array.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array |
Array
|
The array to check. |
||
strictlyNested |
Boolean
|
<optional> |
false | If true, checks if the array is strictly nested, meaning all elements are arrays. |
- Since:
- 1.1.0
True if the array is nested, False otherwise.
Boolean
Example
pastaparse.isNestedArray([1, [2, 3, 4], 5]);
// => true
pastaparse.isNestedArray([1, [2, 3, 4], 5], true);
// => false
pastaparse.isNestedArray([[1, 2], [3, 4, 5], [6]]);
// => true
pastaparse.isNestedArray([[1, 2], [3, 4, 5], [6]], true);
// => true
# static pad(stringopt, lengthopt) → {String}
Pads string on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
String
|
<optional> |
'' | The string to pad. |
length |
Number
|
<optional> |
0 | The padding length. |
- Since:
- 1.0.0
The padded string.
String
Example
pastaparse.pad('hi', 6);
// => ' hi '
pastaparse.pad('hi', 5);
// => ' hi '
pastaparse.pad('hello', 4);
// => 'hello'
# static removeFeetInchChars(feetInchString) → {String}
Removes all feet (') and inch (") unit characters from a string.
Parameters:
Name | Type | Description |
---|---|---|
feetInchString |
String
|
The string to process. |
- Since:
- 1.1.0
The string with any feet and inch characters removed.
String
Example
pastaparse.removeFeetInchChars('2\' - 6"');
// => '2 - 6'
pastaparse.removeFeetInchChars('1\' - 6 1/2"');
// => '1 - 6 1/2'
pastaparse.removeFeetInchChars('0\' 6"');
// => '0 6'
pastaparse.removeFeetInchChars('0\' 6 1/2"');
// => '0 6 1/2'
# static roundToDecimalPlace(number, decimalPlacesopt) → {Number}
Rounds number to the specified decimal places.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
number |
Number
|
The number to round. |
||
decimalPlaces |
Number
|
<optional> |
0 | The number of decimal places to round to. Default is 0. |
- Since:
- 1.1.0
The rounded number.
Number
Example
pastaparse.roundToDecimalPlace(10);
// => 10
pastaparse.roundToDecimalPlace(1.2345, 2);
// => 1.23
pastaparse.roundToDecimalPlace(-1.2345, 2);
// => -1.23
pastaparse.roundToDecimalPlace(1, 4);
// => 1.0000
# static trimTrailingZeroes(numberString) → {String}
Removes trailing zeroes from the end of a number string. i.e. "1.25000" -> "1.25"
Parameters:
Name | Type | Description |
---|---|---|
numberString |
String
|
The number string to trim. |
- Since:
- 1.1.0
If the input string is null or empty.
Error
If the input string is not a valid number string.
Error
The trimmed number string.
String
Example
pastaparse.trimTrailingZeroes('1.25000');
// => '1.25'
pastaparse.trimTrailingZeroes('3.1400');
// => '3.14'
pastaparse.trimTrailingZeroes('5.000');
// => '5'
# static words(stringopt) → {Array.<String>}
Splits string into an array of its words.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
String
|
<optional> |
'' | The string to inspect. |
- Since:
- 1.0.0
The words of string.
Array.<String>
Example
pastaparse.words('I like running in the park');
// => ['I', 'like', 'running', 'in', 'the', 'park']