-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
246 additions
and
225 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
globals: { | ||
"ts-jest": { | ||
tsConfig: "tsconfig.spec.json", | ||
isolatedModules: false, | ||
}, | ||
}, | ||
transform: { | ||
"^.+\\.[jt]s?$": "ts-jest", | ||
}, | ||
testEnvironment: `node`, | ||
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { differenceWith, isEqual } from "lodash" | ||
|
||
type InputObj = Object | Record<string | number | symbol, unknown> | ||
|
||
function compareObjectsByProp( | ||
object1: InputObj, | ||
object2: InputObj, | ||
prop: string | ||
): boolean { | ||
if (Array.isArray(object1[prop])) { | ||
object2[prop] = object2[prop].map(({ _id, ...rest }) => rest) | ||
return differenceWith(object1[prop], object2[prop], isEqual).length === 0 | ||
} else if (typeof object1[prop] === "object") { | ||
delete object2[prop]._id | ||
return isEqual(object1[prop], object2[prop]) | ||
} else { | ||
return object1[prop] === object2[prop] | ||
} | ||
} | ||
|
||
export default compareObjectsByProp |
2 changes: 1 addition & 1 deletion
2
...dusa-core-utils/src/computerize-amount.js → ...dusa-core-utils/src/computerize-amount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ore-utils/src/create-require-from-path.js → ...ore-utils/src/create-require-from-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
.../medusa-core-utils/src/get-config-file.js → .../medusa-core-utils/src/get-config-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dusa-core-utils/src/get-medusa-version.js → ...dusa-core-utils/src/get-medusa-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../medusa-core-utils/src/humanize-amount.js → .../medusa-core-utils/src/humanize-amount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
type ComputePropertyNames< | ||
T, | ||
TKey extends keyof T & string, | ||
TFields extends (keyof T | string)[] = (keyof T | string)[] | ||
> = TKey extends TFields[number] | ||
? T[TKey] extends string | ||
? `${TKey}_id` | ||
: TKey | ||
: TKey | ||
|
||
/** | ||
* Takes an object and a list of fields to tranform in that object. If the field | ||
* exists on the object and its value is a string it will append `_id` to the | ||
* field name. This is used when allowing API calls to hold either ID or object | ||
* values in the payload. The method returns a new object with the | ||
* transformation. | ||
* @param obj - the object to transform | ||
* @param fields - the fields to apply transformation to | ||
* @returns the transformed object | ||
* | ||
* @example | ||
* ```ts | ||
* const obj = { test: "test", toto: 1, tata: Symbol("tata") } | ||
* const out = transformIdableFields(obj, ["test"]) // out: { test_id: string, toto: number, tata: symbol } | ||
* ``` | ||
*/ | ||
export const transformIdableFields = < | ||
T = Record<string, unknown>, | ||
TFields extends (keyof T | string)[] = (keyof T | string)[], | ||
TOutput = { | ||
[P in ComputePropertyNames<T, keyof T & string, TFields>]: P extends keyof T | ||
? T[P] | ||
: string | ||
} | ||
>( | ||
obj: T, | ||
fields: TFields | ||
): TOutput => { | ||
const ret = { ...obj } | ||
|
||
for (let key of fields) { | ||
key = key as string | ||
if (key in obj && typeof obj[key] === "string") { | ||
ret[`${key.toString()}_id`] = obj[key] | ||
delete ret[key] | ||
} | ||
} | ||
|
||
return ret as unknown as TOutput | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.