tornavida
Tools to make a Javascript developer's life easier
by using npm
:
npm i tornavida
by using yarn
:
yarn add tornavida
This repository has 3 general-purposes packages. Those are:
- math
- text
- util
The priority of tornavida
is to provide convenience for applications that uses ECMAScript modules.
If you want to use tornavida with cjs
( CommonJS ) modules in NodeJS
(nodejs uses commonjs as a module system.)
one thing you must do that follows the syntax as listed below.
For example, we assume that you would like to use a module from the util package.
Let's take a look at the syntax we should follow when using ECMAScript modules.
import { sortAscByField } from 'tornavida/util';
// import { sortAscByField } from 'tornavida'; (1)
const array = [
{id: 10, text: '10'},
{id: 15, text: '15'},
{id: 25, text: '25'}
];
const sortedArray = sortAscByField(array, 'id');
(1) This is also valid but you should use follow the syntax which listed above <tornavida>/<package-name>
. If you don't have any concerns about performance you could also use that import statement without subpackage indicator.
Output
[
{ id: 10, text: '10' },
{ id: 15, text: '15' },
{ id: 25, text: '25' }
]
Let's take a look at the syntax we should follow when using CommonJS modules.
You can import CommonJS modules as follows tornavida/<package-name>/cjs
This syntax is valid for all packages that tornavida has.
const { sortDescByField } = require('tornavida/util/cjs');
const array = [
{ id: 10, text: '10' },
{ id: 15, text: '15' },
{ id: 25, text: '25' }
];
const sortedDescArray = sortDescByField(array, 'id');
Output
[
{ id: 25, text: '25' },
{ id: 15, text: '15' },
{ id: 10, text: '10' }
]
Assume that you're just wondering what is the maximum value of the property that array has.
import { fieldMax } from 'tornavida/math';
const arr = [
{ uuid: 1923 },
{ uuid: 1444 },
{ uuid: 1363 },
{ uuid: 1071 }
];
const maxValueOfGivenProp = fieldMax(arr, 'uuid'); // 1923
random function generates random integer value.
import { random } from 'tornavida/math';
const randomInteger = random(1, 10); // Generates random integers between 1 to 10
MIT © enesusta