From 738b9043a5d16f504cbd1de480e5cecceb12e9dd Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Mon, 13 Feb 2023 14:19:44 +0800 Subject: [PATCH 1/3] feat: add `memory` function --- src/core/standard/index.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/standard/index.ts b/src/core/standard/index.ts index 917de479..1eaf7a1c 100644 --- a/src/core/standard/index.ts +++ b/src/core/standard/index.ts @@ -52,6 +52,14 @@ export function inverse(fn: Function) { return (...parameters: Reversed): Returned => fn(...parameters.reverse()); } +export function memory(initial: T, fn: AnyFunction<[previous: T], void>) { + let check = initial; + return (updated: T) => { + if (identical(check, updated)) return; + fn(check), (check = updated); + }; +} + /** * regexp - implementation of global RegExp constructor with escaped pattern * @param pattern passed in the form of string literal From 5d8e76e9d2e9990d2b5de1498224fa922a387fbf Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 1 Mar 2023 14:45:11 +0800 Subject: [PATCH 2/3] make it an identity function --- src/core/standard/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/standard/index.ts b/src/core/standard/index.ts index c106013d..65e97d10 100644 --- a/src/core/standard/index.ts +++ b/src/core/standard/index.ts @@ -53,10 +53,12 @@ export function inverse(fn: Function) { } export function memory(initial: T, fn: AnyFunction<[previous: T], void>) { - let check = initial; + let previous = initial; return (updated: T) => { - if (identical(check, updated)) return; - fn(check), (check = updated); + if (!identical(previous, updated)) { + fn(previous), (previous = updated); + } + return updated; }; } From edf9b0b3da824a26afe389c757b156c72f812b88 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 1 Mar 2023 14:49:54 +0800 Subject: [PATCH 3/3] update changelog and docs --- CHANGELOG.md | 1 + src/core/README.md | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2f9129..480d0f8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - ([#211](https://github.com/alchemauss/mauss/pull/211)) fix `ntv.keys` and `ntv.entries` always returns `string` +- ([#207](https://github.com/alchemauss/mauss/pull/207)) add `memory` function - ([#205](https://github.com/alchemauss/mauss/pull/205)) add `size` function to `ntv` namespace - ([#204](https://github.com/alchemauss/mauss/pull/204)) add `scope` function - ([#202](https://github.com/alchemauss/mauss/pull/202)) add `create` function to `ntv` namespace diff --git a/src/core/README.md b/src/core/README.md index 0ae77dbb..708538a0 100644 --- a/src/core/README.md +++ b/src/core/README.md @@ -157,6 +157,14 @@ A function to check for values equality between two variables. This will work fo A function that accepts a function and returns the same function with the order of parameters reversed. This can be used in conjunction with `compare` methods to sort the items in ascending values. +## `memory` + +```typescript +export function memory(initial: T, fn: (previous: T) => void): (updated: T) => T; +``` + +A higher-order function that runs a callback function only when the `initial`/previous and `updated` are different. + ## `pipe` A type-safe higher-order function that accepts any number of arguments, it returns a function with the parameters of the first function passed and a return type/value of the last function.