mirror of
https://github.com/zhigang1992/mobx-utils.git
synced 2026-06-12 17:08:09 +08:00
Merge pull request #159 from hc-12/patch-2
Allow memoization of function parameter
This commit is contained in:
@@ -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) {
|
||||
|
||||
9
test/__snapshots__/create-transformer.ts.snap
Normal file
9
test/__snapshots__/create-transformer.ts.snap
Normal file
@@ -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"`;
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user