From c487b4f6dcbfa35f60c575b5049321bf4be06534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 18 Apr 2021 12:13:49 +0200 Subject: [PATCH 1/2] implement cancel method to throttle --- packages/function-throttle/index.js | 32 +++++++++++++++++++++-------- test/function-throttle/index.js | 15 ++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/function-throttle/index.js b/packages/function-throttle/index.js index bdb49b110..8cb5f256b 100644 --- a/packages/function-throttle/index.js +++ b/packages/function-throttle/index.js @@ -1,35 +1,51 @@ module.exports = throttle; function throttle(fn, interval, options) { - var wait = false; + var timeoutId = null; var leading = (options && options.leading); var trailing = (options && options.trailing); + if (leading == null) { leading = true; // default } + if (trailing == null) { trailing = !leading; //default } + if (leading == true) { trailing = false; // forced because there should be invocation per call } - return function() { - var callNow = leading && !wait; + var cancel = function() { + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } + }; + + var throttleWrapper = function() { + var callNow = leading && !timeoutId; var context = this; var args = arguments; - if (!wait) { - wait = true; - setTimeout(function() { - wait = false; + + if (!timeoutId) { + timeoutId = setTimeout(function() { + timeoutId = null; + if (trailing) { return fn.apply(context, args); } }, interval); } + if (callNow) { callNow = false; - return fn.apply(this, arguments); + return fn.apply(context, args); } }; + + throttleWrapper.cancel = cancel; + + return throttleWrapper; } diff --git a/test/function-throttle/index.js b/test/function-throttle/index.js index 052754eeb..ca0b8473c 100644 --- a/test/function-throttle/index.js +++ b/test/function-throttle/index.js @@ -392,3 +392,18 @@ test('invokes repeatedly when wait is falsey', function(t) { }, 40); }, 40); }); + +test('cancel delayed function', function(t) { + t.plan(1); + + var callCounter = 0; + var fn = throttle(function() { + callCounter++; + }, 200); + + fn.cancel(); + + setTimeout(function() { + t.equal(callCounter, 0); + }, 400); +}); From 498eaa2fd2e91699f9912aaad64b58fa096c9265 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Goncalves Date: Sun, 11 Feb 2024 13:17:07 +0100 Subject: [PATCH 2/2] creates isNumeric package --- md-variables.json | 15 ++++++++++ packages/string-is-numeric/LICENSE | 21 ++++++++++++++ packages/string-is-numeric/README.md | 29 +++++++++++++++++++ packages/string-is-numeric/index.cjs | 23 +++++++++++++++ packages/string-is-numeric/index.d.ts | 2 ++ packages/string-is-numeric/index.mjs | 25 +++++++++++++++++ packages/string-is-numeric/index.tests.ts | 19 +++++++++++++ packages/string-is-numeric/package.json | 34 +++++++++++++++++++++++ test/string-is-numeric/index.cjs | 19 +++++++++++++ 9 files changed, 187 insertions(+) create mode 100644 packages/string-is-numeric/LICENSE create mode 100644 packages/string-is-numeric/README.md create mode 100644 packages/string-is-numeric/index.cjs create mode 100644 packages/string-is-numeric/index.d.ts create mode 100644 packages/string-is-numeric/index.mjs create mode 100644 packages/string-is-numeric/index.tests.ts create mode 100644 packages/string-is-numeric/package.json create mode 100644 test/string-is-numeric/index.cjs diff --git a/md-variables.json b/md-variables.json index 568053e97..27f74badc 100644 --- a/md-variables.json +++ b/md-variables.json @@ -89,6 +89,21 @@ "intersect([1, 2, 2, 4, 5], [3, 2, 2, 5, 7]); // [2, 5] " ] }, + "just-is-numeric": { + "packageName": "just-is-numeric", + "dir": "string-is-numeric", + "description": "Checks whether a value is numeric", + "examples": [ + "import isNumeric from 'just-is-numeric';", + "", + "isNumeric(3); // true", + "isNumeric('3'); // true", + "isNumeric(3.4); // true", + "isNumeric('3.4'); // true", + "isNumeric('3.4.5'); // false", + "isNumeric('3a'); // false" + ] + }, "just-last": { "packageName": "just-last", "dir": "array-last", diff --git a/packages/string-is-numeric/LICENSE b/packages/string-is-numeric/LICENSE new file mode 100644 index 000000000..30c9ed745 --- /dev/null +++ b/packages/string-is-numeric/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2024 Evandro Goncalves + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/string-is-numeric/README.md b/packages/string-is-numeric/README.md new file mode 100644 index 000000000..9752795af --- /dev/null +++ b/packages/string-is-numeric/README.md @@ -0,0 +1,29 @@ + + + +## just-is-numeric + +Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing. +Guilt-free utilities for every occasion. + +[`🍦 Try it`](https://anguscroll.com/just/just-is-numeric) + +```shell +npm install just-is-numeric +``` +```shell +yarn add just-is-numeric +``` + +Checks whether a value is numeric + +```js +import isNumeric from 'just-is-numeric'; + +isNumeric(3); // true +isNumeric('3'); // true +isNumeric(3.4); // true +isNumeric('3.4'); // true +isNumeric('3.4.5'); // false +isNumeric('3a'); // false +``` diff --git a/packages/string-is-numeric/index.cjs b/packages/string-is-numeric/index.cjs new file mode 100644 index 000000000..b31de1877 --- /dev/null +++ b/packages/string-is-numeric/index.cjs @@ -0,0 +1,23 @@ +module.exports = isNumeric; + +/* + isNumeric(""); // false + isNumeric("123"); // true + isNumeric("123.123"); // true + isNumeric("123.123.123"); // false + isNumeric("abc"); // false + isNumeric("1e8"); // true + isNumeric("1e-8"); // true + isNumeric("1e-8.1"); // false + isNumeric("1aa"); // false +*/ + +function isNumeric(str) { + var type = typeof str; + + return ( + (type === 'string' || type === 'number') && + !isNaN(parseFloat(str)) && + isFinite(str) + ); +} diff --git a/packages/string-is-numeric/index.d.ts b/packages/string-is-numeric/index.d.ts new file mode 100644 index 000000000..7eeed3f4e --- /dev/null +++ b/packages/string-is-numeric/index.d.ts @@ -0,0 +1,2 @@ +declare function isNumeric(value: string | number): boolean; +export default isNumeric; diff --git a/packages/string-is-numeric/index.mjs b/packages/string-is-numeric/index.mjs new file mode 100644 index 000000000..25b78b96f --- /dev/null +++ b/packages/string-is-numeric/index.mjs @@ -0,0 +1,25 @@ +var stringIsNumeric = isNumeric; + +/* + isNumeric(""); // false + isNumeric("123"); // true + isNumeric("123.123"); // true + isNumeric("123.123.123"); // false + isNumeric("abc"); // false + isNumeric("1e8"); // true + isNumeric("1e-8"); // true + isNumeric("1e-8.1"); // false + isNumeric("1aa"); // false +*/ + +function isNumeric(str) { + var type = typeof str; + + return ( + (type === 'string' || type === 'number') && + !isNaN(parseFloat(str)) && + isFinite(str) + ); +} + +export {stringIsNumeric as isNumeric}; diff --git a/packages/string-is-numeric/index.tests.ts b/packages/string-is-numeric/index.tests.ts new file mode 100644 index 000000000..1b193c82c --- /dev/null +++ b/packages/string-is-numeric/index.tests.ts @@ -0,0 +1,19 @@ +import isNumeric from './index'; + +// OK +isNumeric('1'); +isNumeric(1); +isNumeric('1.1'); +isNumeric(1.1); + +// Not OK +// @ts-expect-error +isNumeric([]); +// @ts-expect-error +isNumeric({}); +// @ts-expect-error +isNumeric(); +// @ts-expect-error +isNumeric(false); +// @ts-expect-error +isNumeric(null); diff --git a/packages/string-is-numeric/package.json b/packages/string-is-numeric/package.json new file mode 100644 index 000000000..0ef410ec6 --- /dev/null +++ b/packages/string-is-numeric/package.json @@ -0,0 +1,34 @@ +{ + "name": "is-numeric", + "version": "1.0.0", + "description": "Checks whether a value is numeric", + "type": "module", + "exports": { + ".": { + "types": "./index.d.ts", + "require": "./index.cjs", + "import": "./index.mjs" + }, + "./package.json": "./package.json" + }, + "main": "index.cjs", + "types": "index.d.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "rollup -c" + }, + "repository": "https://github.com/angus-c/just", + "keywords": [ + "number", + "string", + "float", + "integer", + "no-dependencies", + "just" + ], + "author": "evandrolg", + "license": "MIT", + "bugs": { + "url": "https://github.com/angus-c/just/issues" + } +} diff --git a/test/string-is-numeric/index.cjs b/test/string-is-numeric/index.cjs new file mode 100644 index 000000000..9b1d010a5 --- /dev/null +++ b/test/string-is-numeric/index.cjs @@ -0,0 +1,19 @@ +var test = require('../util/test.js')(__filename); +var isNumeric = require('../../packages/string-is-numeric'); + +test('detects invalid cases', function(t) { + t.false(isNumeric('')); + t.false(isNumeric('123.123.123')); + t.false(isNumeric('abc')); + t.false(isNumeric('1e-8.1')); + t.false(isNumeric('1aa')); + t.end(); +}); + +test('detects valid cases', function(t) { + t.true(isNumeric('123')); + t.true(isNumeric('123.123')); + t.true(isNumeric('1e8')); + t.true(isNumeric('1e-8')); + t.end(); +});