{"_id":"@getflywheel/memoize-one-ts","_rev":"55-b04ae3dba7fc734774f60b848f1198ba","name":"@getflywheel/memoize-one-ts","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.2":{"name":"@getflywheel/memoize-one-ts","version":"0.0.2","keywords":["memoization","memoize","cache","typescript","javascript"],"author":"","license":"MIT","_id":"@getflywheel/memoize-one-ts@0.0.2","maintainers":[{"name":"flycrum","email":"chris.crum@getflywheel.com"},{"name":"joshlawler","email":"joshlawler7@gmail.com"},{"name":"afilbert","email":"aron.filbert@getflywheel.com"},{"name":"trevanhetzel","email":"trevan@hetzelcreative.com"},{"name":"claygriffiths","email":"clay@claygriffiths.com"}],"homepage":"https://github.com/getflywheel/memoize-one-ts#readme","bugs":{"url":"https://github.com/getflywheel/memoize-one-ts/issues"},"dist":{"shasum":"6578948abf140f568736a92ec0940b7a81d8f773","tarball":"https://registry.npmjs.org/@getflywheel/memoize-one-ts/-/memoize-one-ts-0.0.2.tgz","fileCount":10,"integrity":"sha512-/GoWclh5ImAx0DJ0CXoASxiEXAP/iL+ZRzKrrQmnjlfWPdJE/t2F4FxagBLccgPZvxX9Ror1M4zHBILllpV7AQ==","signatures":[{"sig":"MEYCIQCOMx2B7SsvcVH9iAjmQMMqAUuZpvWKIqVJlGzdFAJpfAIhAJF3npWynKTEETqOah0eftzp2ezEJ3pR99kEjcquSutm","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":152531,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJco88CCRA9TVsSAnZWagAAOkEP/0iro9gIgMq97Z1kUa4R\nZDY2EH6fep4RggdG2J0wT76YpI8tLzVRZoyCAFqNDlZzBHPQ4m/0mD3UwQHc\nN7JDgvimiJX8Jv8jH9OxTxb2lSo5GMGx8DV1PMwX9xg36hiJql68HuGFZOBa\nj7SwTdPFfpJGom2zrnhf1il1UbnhBIa2NISrfT6fnUO0abOhp7ceVTn+kHtc\nuNBJIMKDUJF0+M58HiR/SXBuGoBxxo871uAiKU1TUpQ+Ok9wSBpj2FcWunhV\nbYxX+f273DrrhjHrkMUwDtrVrhN9RLocMY+joGmWUjHoI9d0hXkDN2dWkrkN\n8Zc6sd2bjj13gH/XurnOE+6bFqYSPt4Hzgxuxx220HBYr62AUI+ixsaEpd2o\nNH8PCFsc3sVpq4CS6MwQM5vg4KQF4fmNKkLtQ8/SVVu6g7F7eBLjun/gb8E1\n5XIOe7vp29mxf/ozdxhbEDr8EpZuRMgTE0uREindfXdfVgwwWZDvcu1NgAOJ\nHzkpcZMywls+DTcswIaW6iPGbfHgL+XSzxDVkoOwVms8LB8QUBA4WEJHajsa\n9wwAtfRAu/+oruX1R4spSgBRlmCMFtTMStzv2Mh07n5JunEZlDuqdGQbHLhM\n8HDpLNq+Ms1VLRuWt5viuq2sC32nAuYrirjiEj6rMjRLM0fo4xqLofq3unYe\ne9eT\r\n=Pi2O\r\n-----END PGP SIGNATURE-----\r\n"},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"a4b22a9ff6a406b6b0c5808372b594ca7583b284","scripts":{"test":"jest","build":"tsc --project tsconfig.json","publish":"npm publish","prerelease":"yarn test && yarn build"},"_npmUser":{"name":"flycrum","email":"chris.crum@getflywheel.com"},"repository":{"url":"git+https://github.com/getflywheel/memoize-one-ts.git","type":"git"},"_npmVersion":"6.4.1","description":"A memoization library which only remembers the latest invocation. Built with TypeScript and includes decorators.","directories":{},"_nodeVersion":"8.15.0","_hasShrinkwrap":false,"devDependencies":{"jest":"^24.5.0","ts-jest":"^24.0.0","typescript":"3.3.4000","@types/jest":"^24.0.11","reflect-metadata":"^0.1.12"},"_npmOperationalInternal":{"tmp":"tmp/memoize-one-ts_0.0.2_1554239233525_0.569156327600677","host":"s3://npm-registry-packages"}}},"time":{"created":"2019-04-02T21:07:13.524Z","modified":"2026-09-04T18:58:22.811Z","0.0.2":"2019-04-02T21:07:13.674Z"},"bugs":{"url":"https://github.com/getflywheel/memoize-one-ts/issues"},"license":"MIT","homepage":"https://github.com/getflywheel/memoize-one-ts#readme","keywords":["memoization","memoize","cache","typescript","javascript"],"repository":{"url":"git+https://github.com/getflywheel/memoize-one-ts.git","type":"git"},"description":"A memoization library which only remembers the latest invocation. Built with TypeScript and includes decorators.","maintainers":[{"email":"aron.filbert@getflywheel.com","name":"afilbert"},{"email":"matt.lawrence@wpengine.com","name":"mattlawck"},{"email":"nathan.pike@wpengine.com","name":"nathan.pike"},{"email":"april.neal@wpengine.com","name":"april.neal"},{"email":"spencer.putnam@wpengine.com","name":"spwpe"},{"email":"michael.david@wpengine.com","name":"mdavid-fw"},{"email":"benjamin@passionsplay.com","name":"passionsplay"},{"email":"jeremy.pollock@wpengine.com","name":"jpollock_wpe"},{"email":"nick@cern.is","name":"ndc"},{"email":"joshua.pregua@wpengine.com","name":"jpreguawpengine"},{"email":"nathan.rice@wpengine.com","name":"nathanrice"},{"email":"michael.day@wpengine.com","name":"mike-day"},{"email":"joseph.villafranca@wpengine.com","name":"jvwpengine"},{"email":"john.caldwell@wpengine.com","name":"john.caldwell"},{"email":"nicholascpetersen@gmail.com","name":"nicholaspetersen"},{"email":"austin.wendt@wpengine.com","name":"austin.wendt"},{"email":"jimvirle.calago@wpengine.com","name":"jim.wpengine"}],"readme":"# memoize-one\n\nA memoization library that only caches the result of the most recent arguments. \nThis a fork of Alex Reardon's fantastic [memoize-one](https://www.google.com) JavaScript library, but is written in TypeScript and utilizes decorators to better facilitate our [Local by Flywheel](https://localbyflywheel.com/) coding efforts.\n\n## Rationale\n\nUnlike other memoization libraries, `memoize-one-ts` only remembers the latest arguments and result. \nNo need to worry about cache busting mechanisms such as `maxAge`, `maxSize`, `exclusions` and so on which can be prone to memory leaks. \n`memoize-one-ts` simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result.\n\n## Usage\n\n### Standard usage\n\n```typescript\nimport memoizeOne from 'memoize-one-ts';\n\nconst add = (a, b) => a + b;\nconst memoizedAdd = memoizeOne(add);\n\nmemoizedAdd(1, 2); // 3\n\nmemoizedAdd(1, 2); // 3\n// Add function is not executed: previous result is returned\n\nmemoizedAdd(2, 3); // 5\n// Add function is called to get new value\n\nmemoizedAdd(2, 3); // 5\n// Add function is not executed: previous result is returned\n\nmemoizedAdd(1, 2); // 3\n// Add function is called to get new value.\n// While this was previously cached,\n// it is not the latest so the cached result is lost\n```\n\n## Installation\n\n```bash\n# yarn\nyarn add memoize-one-ts\n\n# npm\nnpm install memoize-one-ts --save\n```\n\n## Custom equality function\n\nYou can also pass in a custom function for checking the equality of two sets of arguments\n\n```typescript\nconst memoized = memoizeOne(fn, isEqual);\ntype EqualityFn = (newArgs: any[], oldArgs: any[]) => boolean;\n```\n\nAn equality function should return `true` if the arguments are equal. If `true` is returned then the wrapped function will not be called.\n\nThe default equality function is a shallow equal check of all arguments (each argument is compared with `===`). The default equality function also does not check anything if the length of the arguments changes. You are welcome to decide if you want to return `false` if the `length` of the arguments is not equal\n\n```typescript\nconst simpleIsEqual: EqualityFn = (\n  newArgs: any[],\n  lastArgs: any[],\n): boolean =>\n  newArgs.length === lastArgs.length &&\n  newArgs.every(\n    (newArg: any, index: number): boolean =>\n      shallowEqual(newArg, lastArgs[index]),\n  );\n```\n\nA custom equality function needs to compare `Arrays`. The `newArgs` array will be a new reference every time so a simple `newArgs === lastArgs` will always return `false`.\n\nEquality functions are not called if the `this` context of the function has changed (see below).\n\nHere is an example that uses a `lodash.isequal` deep equal equality check\n\n> `lodash.isequal` correctly handles deep comparing two arrays\n\n```typescript\nimport memoizeOne from 'memoize-one-ts';\nimport isDeepEqual from 'lodash.isequal';\n\nconst identity = x => x;\n\nconst shallowMemoized = memoizeOne(identity);\nconst deepMemoized = memoizeOne(identity, isDeepEqual);\n\nconst result1 = shallowMemoized({ foo: 'bar' });\nconst result2 = shallowMemoized({ foo: 'bar' });\n\nresult1 === result2; // false - difference reference\n\nconst result3 = deepMemoized({ foo: 'bar' });\nconst result4 = deepMemoized({ foo: 'bar' });\n\nresult3 === result4; // true - arguments are deep equal\n```\n\n## `this`\n\n### `memoize-one-ts` correctly respects `this` control\n\nThis library takes special care to maintain, and allow control over the the `this` context for **both** the original function being memoized as well as the returned memoized function. \nBoth the original function and the memoized function's `this` context respect [all the `this` controlling techniques](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md):\n\n- new bindings (`new`)\n- explicit binding (`call`, `apply`, `bind`);\n- implicit binding (call site: `obj.foo()`);\n- default binding (`window` or `undefined` in `strict mode`);\n- fat arrow binding (binding to lexical `this`)\n- ignored this (pass `null` as `this` to explicit binding)\n\n### Changes to `this` is considered an argument change\n\nChanges to the running context (`this`) of a function can result in the function returning a different value even though its arguments have stayed the same:\n\n```typescript\nfunction getA() {\n  return this.a;\n}\n\nconst temp1 = {\n  a: 20,\n};\nconst temp2 = {\n  a: 30,\n};\n\ngetA.call(temp1); // 20\ngetA.call(temp2); // 30\n```\n\nTherefore, in order to prevent against unexpected results, `memoize-one-ts` takes into account the current execution context (`this`) of the memoized function. \nIf `this` is different to the previous invocation then it is considered a change in argument. [further discussion](https://github.com/alexreardon/memoize-one/issues/3).\n\nGenerally this will be of no impact if you are not explicity controlling the `this` context of functions you want to memoize with [explicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding) or [implicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#implicit-binding). \n`memoize-one-ts` will detect when you are manipulating `this` and will then consider the `this` context as an argument. \nIf `this` changes, it will re-execute the original function even if the arguments have not changed.\n\n## When your result function `throw`s\n\n> There is no caching when your result function throws\n\nIf your result function `throw`s then the memoized function will also throw. \nThe throw will not break the memoized functions existing argument cache. \nIt means the memoized function will pretend like it was never called with arguments that made it `throw`.\n\n```typescript\nconst canThrow = (name: string) => {\n  console.log('called');\n  if (name === 'throw') {\n    throw new Error(name);\n  }\n  return { name };\n};\n\nconst memoized = memoizeOne(canThrow);\n\nconst value1 = memoized('Alex');\n// console.log => 'called'\nconst value2 = memoized('Alex');\n// result function not called\n\nconsole.log(value1 === value2);\n// console.log => true\n\ntry {\n  memoized('throw');\n  // console.log => 'called'\n} catch (e) {\n  firstError = e;\n}\n\ntry {\n  memoized('throw');\n  // console.log => 'called'\n  // the result function was called again even though it was called twice\n  // with the 'throw' string\n} catch (e) {\n  secondError = e;\n}\n\nconsole.log(firstError !== secondError);\n\nconst value3 = memoized('Alex');\n// result function not called as the original memoization cache has not been busted\nconsole.log(value1 === value3);\n// console.log => true\n```\n","readmeFilename":"README.md"}