From 9e6549b1530cf4eb78fb36e9dfa6cf3566ab7b6f Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 5 Feb 2022 18:14:35 +0100 Subject: [PATCH] Update format --- Makefile | 7 ++ README.md | 31 ++++++- dist/fn-tools.js | 105 ---------------------- dist/fnTools.js | 133 +++++++++++++++++++++++++++ fn-tools.ts => src/fnTools.ts | 165 +++++++++++++++++++--------------- 5 files changed, 260 insertions(+), 181 deletions(-) create mode 100644 Makefile delete mode 100644 dist/fn-tools.js create mode 100644 dist/fnTools.js rename fn-tools.ts => src/fnTools.ts (77%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..492dfbc --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.DEFAULT_GOAL := help + +help: + @perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}' + +build: ## Build in 'dist' + tsc src/fnTools.ts -t es2017 --module amd -outFile dist/fnTools.js diff --git a/README.md b/README.md index 929f04d..0c9ee0d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,31 @@ +# fnTools -# Compile +Simple functional functions common to any development in Typescript or JavaScript. + +- **inc**: Increase the value of a `number`. +- **dec**: Decrease the value of a `number`. +- **range**: Returns an array with a sequence. +- **uniqValuesArray**: Creates a duplicate-free version of an array. +- **cloneJSON**: Clone JSON. +- **updateJSON**: Returns a JSON with an updated value. + +## Example + +```typescript +``` + +## Compile + +Install Typescript ```shell -tsc fn-tools.ts -t es2017 -outFile dist/fn-tools.js -```` \ No newline at end of file +npm install -g typescript +``` + +And build. + +```shell +make build +```` + +Check 'dist' folder. \ No newline at end of file diff --git a/dist/fn-tools.js b/dist/fn-tools.js deleted file mode 100644 index 9fbb6e0..0000000 --- a/dist/fn-tools.js +++ /dev/null @@ -1,105 +0,0 @@ -/** - * Returns a JSON with an updated value - * @param {string} key - * @param {string} newValue - * @param {JSON} json - * @return {JSON} - * @example - * - * const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; - * updateJSON('name', 'maria', person); - * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} - * - * Others: "modify" in Ramda - */ -function updateJSON(key, newValue, json) { - // JSON to Array - return Object.entries(json).map(function (row) { - // Replace value - return row[0] == key ? row.slice(0, 1).concat(newValue) : row; - }).reduce(function (jsonUpdate, valueUpdate) { - // Array to JSON - jsonUpdate[valueUpdate[0]] = valueUpdate[1]; - return jsonUpdate; - }, {}); -} -/** - * Returns an array with a sequence. - * - * @param {number} start - Beginning of the range. - * @param {number} stop - End of range. - * @param {number} step - Interval between numbers. Default is 1. - * @return {Array} Sequence. - * @example - * - * range(4) - * // => [0, 1, 2, 3, 4] - * - * range(2, 4) - * // => [2, 3, 4] - * - * range(0, 9, 3) - * // => [0, 3, 6, 9] - */ -function range(start, stop = null, step = 1) { - const startArray = stop === null ? 0 : start; - const stopArray = stop === null ? start : stop; - return Array.from({ length: (stopArray - startArray) / step + 1 }, (_, i) => startArray + (i * step)); -} -/** - * Creates a duplicate-free version of an array. - * - * @param {Array} array - The array to inspect. - * @return {Array} - Returns the new duplicate free array. - * @example - * - * uniqValuesArray([2, 1, 2, 2]) - * // => [2, 1] - */ -function uniqValuesArray(array) { - return Array.from(new Set(array)); -} -/** - * Increase the value of a `number`. - * - * @param {number} number The number to increase. - * @param {number} number Increase. - * @return {number} Returns the decrease. - * @example - * - * inc(4) - * // => 5 - * - * inc(5, 3) - * // => 8 - * - * dec(7.3) - * // => 8.3 - * - */ -function inc(number, increase = 1) { - return number + increase; -} -/** - * Decrease the value of a `number`. - * - * @param {number} number - The number to decrease. - * @param {number} number - Decrease. - * @return {number} Returns the decrease. - * @example - * - * dec(4) - * // => 3 - * - * dec(5, 3) - * // => 2 - * - * dec(7.3) - * // => 6.3 - * - * dec("foo") - * // => false - */ -function dec(number, decrease = 1) { - return number - decrease; -} diff --git a/dist/fnTools.js b/dist/fnTools.js new file mode 100644 index 0000000..7d6f00a --- /dev/null +++ b/dist/fnTools.js @@ -0,0 +1,133 @@ +define("fnTools", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.cloneJSON = exports.uniqValuesArray = exports.updateJSON = exports.range = exports.dec = exports.inc = void 0; + /** + * Increase the value of a `number`. + * + * @param {number} number The number to increase. + * @param {number} number Increase. + * @return {number} Returns the decrease. + * @example + * + * inc(4) + * // => 5 + * + * inc(5, 3) + * // => 8 + * + * dec(7.3) + * // => 8.3 + * + */ + function inc(number, increase = 1) { + return number + increase; + } + exports.inc = inc; + /** + * Decrease the value of a `number`. + * + * @param {number} number - The number to decrease. + * @param {number} number - Decrease. + * @return {number} Returns the decrease. + * @example + * + * dec(4) + * // => 3 + * + * dec(5, 3) + * // => 2 + * + * dec(7.3) + * // => 6.3 + * + * dec("foo") + * // => false + */ + function dec(number, decrease = 1) { + return number - decrease; + } + exports.dec = dec; + /** + * Returns an array with a sequence. + * + * @param {number} start - Beginning of the range. + * @param {number} stop - End of range. + * @param {number} step - Interval between numbers. Default is 1. + * @return {Array} Sequence. + * @example + * + * range(4) + * // => [0, 1, 2, 3, 4] + * + * range(2, 4) + * // => [2, 3, 4] + * + * range(0, 9, 3) + * // => [0, 3, 6, 9] + */ + function range(start, stop = null, step = 1) { + const startArray = stop === null ? 0 : start; + const stopArray = stop === null ? start : stop; + return Array.from({ length: (stopArray - startArray) / step + 1 }, (_, i) => startArray + (i * step)); + } + exports.range = range; + /** + * Returns a JSON with an updated value + * @param {string} key + * @param {string} newValue + * @param {JSON} json + * @return {JSON} + * @example + * + * const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; + * updateJSON('name', 'maria', person); + * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} + * + * Others: "modify" in Ramda + */ + function updateJSON(key, newValue, json) { + // JSON to Array + return Object.entries(json).map(function (row) { + // Replace value + return row[0] == key ? row.slice(0, 1).concat(newValue) : row; + }).reduce(function (jsonUpdate, valueUpdate) { + // Array to JSON + jsonUpdate[valueUpdate[0]] = valueUpdate[1]; + return jsonUpdate; + }, {}); + } + exports.updateJSON = updateJSON; + /** + * Creates a duplicate-free version of an array. + * + * @param {Array} array - The array to inspect. + * @return {Array} - Returns the new duplicate free array. + * @example + * + * uniqValuesArray([2, 1, 2, 2]) + * // => [2, 1] + */ + function uniqValuesArray(array) { + return Array.from(new Set(array)); + } + exports.uniqValuesArray = uniqValuesArray; + /** + * Clone JSON. + * + * @param {JSON} json - Object. Default {}. + * @return {JSON} New object. + * @example + * + * cloneObject({"edad": 23}) + * // => {"edad": 23} + * + * cloneObject() + * // => {} + * + */ + function cloneJSON(json) { + return JSON.parse(JSON.stringify(json)); + } + exports.cloneJSON = cloneJSON; +}); diff --git a/fn-tools.ts b/src/fnTools.ts similarity index 77% rename from fn-tools.ts rename to src/fnTools.ts index 0afc54c..89e9563 100644 --- a/fn-tools.ts +++ b/src/fnTools.ts @@ -1,73 +1,3 @@ -/** - * Returns a JSON with an updated value - * @param {string} key - * @param {string} newValue - * @param {JSON} json - * @return {JSON} - * @example - * - * const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; - * updateJSON('name', 'maria', person); - * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} - * - * Others: "modify" in Ramda - */ -function updateJSON(key: string, newValue: any, json: JSON): JSON { - // JSON to Array - return Object.entries(json).map( - function (row: any[]): any[] { - // Replace value - return row[0] == key ? row.slice(0, 1).concat(newValue) : row - } - ).reduce( - function (jsonUpdate: any, valueUpdate: any): JSON { - // Array to JSON - jsonUpdate[valueUpdate[0]] = valueUpdate[1]; - return jsonUpdate; - }, {}); -} - - -/** - * Returns an array with a sequence. - * - * @param {number} start - Beginning of the range. - * @param {number} stop - End of range. - * @param {number} step - Interval between numbers. Default is 1. - * @return {Array} Sequence. - * @example - * - * range(4) - * // => [0, 1, 2, 3, 4] - * - * range(2, 4) - * // => [2, 3, 4] - * - * range(0, 9, 3) - * // => [0, 3, 6, 9] - */ -function range(start: number, stop: number | null=null, step: number=1): number[] { - const startArray = stop === null ? 0 : start; - const stopArray = stop === null ? start : stop; - return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step)); -} - - -/** - * Creates a duplicate-free version of an array. - * - * @param {Array} array - The array to inspect. - * @return {Array} - Returns the new duplicate free array. - * @example - * - * uniqValuesArray([2, 1, 2, 2]) - * // => [2, 1] - */ -function uniqValuesArray(array: any[]): any[] { - return Array.from(new Set(array)); -} - - /** * Increase the value of a `number`. * @@ -82,11 +12,11 @@ function uniqValuesArray(array: any[]): any[] { * inc(5, 3) * // => 8 * - * dec(7.3) + * inc(7.3) * // => 8.3 * */ -function inc(number: number, increase: number=1): number { +export function inc(number: number, increase: number=1): number { return number + increase; } @@ -111,6 +41,95 @@ function inc(number: number, increase: number=1): number { * dec("foo") * // => false */ -function dec(number: number, decrease: number=1): number { +export function dec(number: number, decrease: number=1): number { return number - decrease; } + + +/** + * Returns an array with a sequence. + * + * @param {number} start - Beginning of the range. + * @param {number} stop - End of range. + * @param {number} step - Interval between numbers. Default is 1. + * @return {Array} Sequence. + * @example + * + * range(4) + * // => [0, 1, 2, 3, 4] + * + * range(2, 4) + * // => [2, 3, 4] + * + * range(0, 9, 3) + * // => [0, 3, 6, 9] + */ +export function range(start: number, stop: number | null=null, step: number=1): number[] { + const startArray = stop === null ? 0 : start; + const stopArray = stop === null ? start : stop; + return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step)); +} + + +/** + * Returns a JSON with an updated value + * @param {string} key + * @param {string} newValue + * @param {JSON} json + * @return {JSON} + * @example + * + * const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; + * updateJSON('name', 'maria', person); + * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} + * + * Others: "modify" in Ramda + */ +export function updateJSON(key: string, newValue: any, json: JSON): JSON { + // JSON to Array + return Object.entries(json).map( + function (row: any[]): any[] { + // Replace value + return row[0] == key ? row.slice(0, 1).concat(newValue) : row + } + ).reduce( + function (jsonUpdate: any, valueUpdate: any): JSON { + // Array to JSON + jsonUpdate[valueUpdate[0]] = valueUpdate[1]; + return jsonUpdate; + }, {}); +} + + +/** + * Creates a duplicate-free version of an array. + * + * @param {Array} array - The array to inspect. + * @return {Array} - Returns the new duplicate free array. + * @example + * + * uniqValuesArray([2, 1, 2, 2]) + * // => [2, 1] + */ +export function uniqValuesArray(array: any[]): any[] { + return Array.from(new Set(array)); +} + + +/** + * Clone JSON. + * + * @param {JSON} json - Object. Default {}. + * @return {JSON} New object. + * @example + * + * cloneObject({"edad": 23}) + * // => {"edad": 23} + * + * cloneObject() + * // => {} + * + */ +export function cloneJSON(json: JSON): JSON { + return JSON.parse(JSON.stringify(json)); +} \ No newline at end of file