A collection of helpful JavaScript functions, started by TogaTech.org and built by the open-source community
To contribute to helpful.js, please see the contributing guide.
Website: helpful.js.org
For the browser, only ./helpful.js
or ./helpful.min.js
is required and has all dependencies bundled inside the single file. We recommend including ./helpful.min.js.map
in the same directory as ./helpful.min.js
, which allows a browser to reconstruct the original unminified file in the debugger.
<script type="text/javascript" src="./helpful.min.js"></script>
<script>
helpful.stringToArray("hello");
helpful.duplicateArray([0, 1, 1, 3, 5]);
...
</script>
Helpful.js is available through the NPM registry. To install helpful.js, use the following command in the terminal:
npm install @togatech/helpful-js
Make sure to run the test cases to ensure that helpful.js works properly:
npm test
To include helpful.js in your code:
const helpful = require("@togatech/helpful-js");
helpful.stringToArray("hello");
helpful.duplicateArray([0, 1, 1, 3, 5]);
...
To include specific helpful.js methods in your code:
const { stringToArray, duplicateArray } = require("@togatech/helpful-js");
stringToArray("hello");
duplicateArray([0, 1, 1, 3, 5]);
...
For node.js file import, place the ./helpful.js
or ./helpful.min.js
file in your project directory.
Make sure to run the test cases to ensure that helpful.js works properly (you will need to add the ./test
folder to the project directory):
npm install mocha --save-dev
mocha
To include helpful.js in your code:
const helpful = require("./helpful.min.js");
helpful.stringToArray("hello");
helpful.duplicateArray([0, 1, 1, 3, 5]);
...
To include specific helpful.js methods in your code:
const { stringToArray, duplicateArray } = require("./helpful.min.js");
stringToArray("hello");
duplicateArray([0, 1, 1, 3, 5]);
...
If you would like to minify the code yourself instead of using the provided tenvoy.min.js
(and optional tenvoy.min.js.map
), you can use UglifyJS 3 to minifiy the code yourself.
To install UglifyJS 3 as a command line app through NPM, run npm install uglify-js -g
.
After UglifyJS 3 has been installed, you can run the following commands in your terminal:
uglifyjs ./helpful.js -o ./helpful.min.js -c -m --source-map "filename='./helpful.min.js.map',url='helpful.min.js.map'"
To contribute to helpful.js, please see the contributing guide and open a pull request. We look forward to reviewing your contributions!
Converts a string into an array of individual characters
let array = helpful.stringToArray("test"); // ["t", "e", "s", "t"]
Parameters:
- string: string (
"test"
)
Return Type: Array (["t", "e", "s", "t"]
)
Duplicates an array by creating a new array and transferring all the elements from the first array
let array = helpful.duplicateArray(["t", "e", "s", "t"]); // ["t", "e", "s", "t"]
Parameters:
- array: Array (
["t", "e", "s", "t"]
)
Return Type: Array (["t", "e", "s", "t"]
)
Finds the difference between two arrays (any identical elements in the second array are removed from the first array)
let differenceArray = helpful.differenceOfArrays([2, 1], [2, 3]); // [1]
Parameters:
- array1: Array (
[2, 1]
) - array2: Array (
[2, 3]
)
Return Type: Array (Array [1]
)
Finds the sum of two arrays (the two arrays are combined)
let sumArray = helpful.sumOfArrays([1, 2], [3, 4]); // [1, 2, 3, 4]
Parameters:
- array1: Array (
[1, 2]
) - array2: Array (
[3, 4]
)
Return Type: Array (Array [1, 2, 3, 4]
)
Capitalizes the first letter of every word
let capitalizedWord = helpful.capitalize('heLLo'); // Hello
let capitalizedSentence = helpful.capitalize('hello javaScript world!'); // Hello Javascript World!
Parameters:
- string: string (
"heLLo"
)
Return Type: string ("Hello"
)
Merges the second array into the first one (skip duplicated values)
let mergedArray = helpful.mergeArrays([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4]
Parameters:
- array1: Array (
[1, 2, 3]
) - array2: Array (
[2, 3, 4]
)
Return Type: Array (Array [1, 2, 3, 4]
)
Shuffles an array, returning another array with the same values but in a different order
let array = helpful.shuffleArray(["t", "e", "s", "t"]); // ["e", "t", "t", "s"]
Parameters:
- array: Array (
["t", "e", "s", "t"]
)
Return Type: Array(["t", "e", "s", "t"]
)
Reverses an array by creating a new array with the same values in the oposite order
let array = helpful.reverseArray(["t", "e", "s", "t"]); // ["t", "s", "e", "t"]
Parameters:
- array: Array (
["t", "e", "s", "t"]
)
Return Type: Array (["t", "s", "e", "t"]
)
Converts a string to hexadecimal
let hex = helpful.hex.convertFromString("test"); // "74657374"
Parameters:
- string: string (
"test"
)
Return Type: string ("74657374"
)
Converts hexadecimal to a string
let string = helpful.hex.convertToString("74657374"); // "test"
Parameters:
- hex: string (
"74657374"
)
Return Type: string ("test"
)
Converts a bytes array to hexadecimal
let hex = helpful.hex.convertFromBytes(new Uint8Array([116, 101, 115, 116])); // "74657374"
Parameters:
- bytes: Uint8Array (
Uint8Array [116, 101, 115, 116]
)
Return Type: string ("74657374"
)
Converts hexadecimal to a bytes array
let bytes = helpful.hex.convertToBytes("74657374"); // Uint8Array [116, 101, 115, 116]
Parameters:
- hex: string (
"74657374"
)
Return Type: Uint8Array (Uint8Array [116, 101, 115, 116]
)