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

Allow memoization of function parameter #159

Merged
merged 5 commits into from
Nov 19, 2018
Merged
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
9 changes: 5 additions & 4 deletions src/create-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ export function createTransformer<A, B>(
}

function getMemoizationId(object: any) {
if (typeof object === "string") return `string:${object}`
if (typeof object === "number") return `number:${object}`
if (object === null || typeof object !== "object")
const objectType = typeof object
if (objectType === "string") return `string:${object}`
if (objectType === "number") return `number:${object}`
if (object === null || (objectType !== "object" && objectType !== "function"))
throw new Error(
"[mobx-utils] transform expected an object, string or number, got: " + object
`[mobx-utils] transform expected an object, function, string or number, got: ${String(object)}`
)
let tid = object.$transformId
if (tid === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/create-transformer.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should throw error when passed invalid param type 1`] = `"[mobx-utils] transform expected an object, function, string or number, got: null"`;

exports[`should throw error when passed invalid param type 2`] = `"[mobx-utils] transform expected an object, function, string or number, got: undefined"`;

exports[`should throw error when passed invalid param type 3`] = `"[mobx-utils] transform expected an object, function, string or number, got: Symbol(A)"`;

exports[`should throw error when passed invalid param type 4`] = `"[mobx-utils] transform expected an object, function, string or number, got: true"`;
18 changes: 18 additions & 0 deletions test/create-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,3 +1281,21 @@ function createTestSet(): any {

return testSet
}

it("should throw error when passed invalid param type", () => {
const transformedFn = createTransformer((obj: any) => obj)
const validParams = [[], {}, jest.fn(), 1, "string"]
const invalidParams = [null, undefined, Symbol("A"), true]

validParams.forEach(obj => {
expect(() => {
transformedFn(obj)
}).not.toThrowError()
})

invalidParams.forEach(obj => {
expect(() => {
transformedFn(obj)
}).toThrowErrorMatchingSnapshot()
})
})
Loading