Skip to content

Commit

Permalink
Migrate @emotion/weak-memoize to TypeScript (#2429)
Browse files Browse the repository at this point in the history
* [weak-memoize] Convert to TypeScript

* Add changeset

* tweak a bunch of stuff

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
sarayourfriend and Andarist authored Aug 14, 2021
1 parent 7f8db2d commit b1d16b0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-hairs-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/weak-memoize': minor
---

Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.
3 changes: 1 addition & 2 deletions packages/memoize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
},
"files": [
"src",
"dist",
"types/*.d.ts"
"dist"
],
"browser": {
"./dist/emotion-memoize.cjs.js": "./dist/emotion-memoize.browser.cjs.js",
Expand Down
5 changes: 2 additions & 3 deletions packages/weak-memoize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A memoization function that uses a WeakMap",
"main": "dist/emotion-weak-memoize.cjs.js",
"module": "dist/emotion-weak-memoize.esm.js",
"types": "types/index.d.ts",
"types": "dist/declarations/src/index.d.ts",
"license": "MIT",
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/weak-memoize",
"scripts": {
Expand All @@ -18,8 +18,7 @@
},
"files": [
"src",
"dist",
"types/*.d.ts"
"dist"
],
"browser": {
"./dist/emotion-weak-memoize.cjs.js": "./dist/emotion-weak-memoize.browser.cjs.js",
Expand Down
15 changes: 0 additions & 15 deletions packages/weak-memoize/src/index.js

This file was deleted.

17 changes: 17 additions & 0 deletions packages/weak-memoize/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let weakMemoize = function <Arg extends object, Return>(
func: (arg: Arg) => Return
): (arg: Arg) => Return {
let cache = new WeakMap<Arg, Return>()
return (arg: Arg) => {
if (cache.has(arg)) {
// Use non-null assertion because we just checked that the cache `has` it
// This allows us to remove `undefined` from the return value
return cache.get(arg)!
}
let ret = func(arg)
cache.set(arg, ret)
return ret
}
}

export default weakMemoize
6 changes: 1 addition & 5 deletions packages/weak-memoize/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TypeScript Version: 2.2

type UnaryFn<Arg, Return> = (arg: Arg) => Return

export default function weakMemoize<Arg extends object, Return>(
func: UnaryFn<Arg, Return>
): UnaryFn<Arg, Return>
export { default } from '../src'
2 changes: 1 addition & 1 deletion packages/weak-memoize/types/tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import weakMemoize from '@emotion/weak-memoize'
import weakMemoize from '../src'

interface Foo {
bar: 'xyz'
Expand Down
3 changes: 2 additions & 1 deletion packages/weak-memoize/types/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],

"no-unnecessary-generics": false,
"strict-export-declare-modifiers": false
"strict-export-declare-modifiers": false,
"no-default-import": false
}
}

0 comments on commit b1d16b0

Please sign in to comment.