Skip to content

Commit

Permalink
Merge pull request #17 from ryami333/feature/docs
Browse files Browse the repository at this point in the history
Feature/docs
  • Loading branch information
ryami333 committed Jan 16, 2018
2 parents 53bd3f8 + 06d1210 commit 7a68cbb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Mitch Ryan

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.
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Memily

Javascript [https://en.wikipedia.org/wiki/Memoization](memoization) library. Wrap your computationally expensive functions with `memily` so that the results of calls are cached.

### Installing

Install `memily` to your project by running:

```bash
npm install --save memily
```

Or if you use Yarn:

```bash
yarn add memily
```

Then, import memily into your project like this:

```jsx
import memily from 'memily';
```

### Examples

#### Basic Usage

```jsx
import memily from 'memily';

function squareRoot(num) {
console.log('Hello world!');
return Math.sqrt(num);
}

const squareRootMemoized(squareRoot);

squareRootMemoized(4); // console.log: 'Hello world!'
squareRootMemoized(4); // ...
squareRootMemoized(4); // ...
squareRootMemoized(9); // console.log: 'Hello world!'
```

#### Caching for a finite time using `maxAge` option.

```jsx
import memily from 'memily';

function squareRoot(num) {
console.log('Hello world!');
return Math.sqrt(num);
}

const squareRootMemoized(squareRoot, { maxAge: 100 });

Promise.resolve()
.then(() => squareRootMemoized(4)) // console.log: 'Hello world!'
.then(() => squareRootMemoized(4)) // ...
.then(() => new Promise(resolve => setTimeout(resolve, 200)))
.then(() => squareRootMemoized(4)) // console.log: 'Hello world!'
```

#### Caching against a custom key using the `cacheKey` option.

```jsx
import memily from 'memily';

const steveHolt = {
id: 1,
firstName: 'Steve',
surname: 'Holt',
};

const tobiasFunke = {
id: 2,
firstName: 'Tobias',
surname: 'Funke',
};

function getFullNameString(user) {
console.log('Hello world!');
return `${user.firstName} ${user.surname}`;
}

const squareRootMemoized(squareRoot, { cacheKey: user => user.id });

getFullNameString(steveHolt); // console.log: 'Hello world!'
getFullNameString(steveHolt); // ...
getFullNameString(tobiasFunke); // console.log: 'Hello world!'
getFullNameString(tobiasFunke); // ...
```

#### Flushing the memoization cache

```jsx
import { flush } from 'memily';
```

### Flow Types

Memily comes with Flow types built in. No `flow-typed` installation required.

### License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "dist/memily.js",
"repository": "git@github.com:ryami333/memily.git",
"author": "Mitch Ryan <mitch@mitch-ryan.com>",
"license": "UNLICENSED",
"license": "MIT",
"keywords": ["memoization", "mem", "cache", "flow"],
"devDependencies": {
"@babel/cli": "^7.0.0-beta.37",
"@babel/core": "^7.0.0-beta.37",
Expand Down

0 comments on commit 7a68cbb

Please sign in to comment.