Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add memory function #207

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions workspace/mauss/src/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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<T>(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. This is useful for updating the state of a component only when the value has changed.

## `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.
Expand Down
10 changes: 10 additions & 0 deletions workspace/mauss/src/core/standard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export function inverse<Function extends AnyFunction>(fn: Function) {
return (...parameters: Reversed): Returned => fn(...parameters.reverse());
}

export function memory<T>(initial: T, fn: AnyFunction<[previous: T], void>) {
let previous = initial;
return (updated: T) => {
if (!identical(previous, updated)) {
fn(previous), (previous = updated);
}
return updated;
};
}

/**
* regexp - implementation of global RegExp constructor with escaped pattern
* @param pattern passed in the form of string literal
Expand Down