-
-
Notifications
You must be signed in to change notification settings - Fork 96
Convert to TypeScript and ensure strict type safety #101
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
Open
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
482afa4
Update src to full TypeScript
XDGFX 221d586
Convert tests to TypeScript and confirm all passed
XDGFX 35dbfea
Enable declaration generation in tsconfig.json
XDGFX e19ac86
Fix incorrect file extension
XDGFX c520dbb
Fix exports with types
XDGFX 8cceb71
Export types and pass test:coverage
XDGFX e5d4e2f
Change prepublish to prepare script
XDGFX 94439a0
Add .npmignore file to prevent .gitignore removing `dist/`
XDGFX f2e3ea5
Add DiffType
XDGFX 4d98c18
Fix support for esm
XDGFX 11ca18d
Filename and test script fix
XDGFX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Convert tests to TypeScript and confirm all passed
- Loading branch information
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
jest.config.js
This file contains hidden or 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,4 @@ | ||
| module.exports = { | ||
| preset: "ts-jest", | ||
| testEnvironment: "node", | ||
| }; |
This file contains hidden or 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
33 changes: 27 additions & 6 deletions
src/types.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,38 @@ | ||
| type ElementType<T> = T extends unknown[] | ||
| ? T extends (infer U)[] | ||
| ? U | ||
| : never | ||
| : T; | ||
|
|
||
| type EmptyObject = Record<string, never>; | ||
|
|
||
| export type DiffDeletedType<T, U> = | ||
| | { | ||
| [P in Exclude<keyof T, keyof U>]: undefined; | ||
| [P in Exclude<keyof ElementType<T>, keyof ElementType<U>>]: undefined; | ||
| } | ||
| | Record<string, never>; | ||
| | EmptyObject; | ||
|
|
||
| export type DiffAddedType<T, U> = | ||
| | { | ||
| [P in Exclude<keyof U, keyof T>]: U[P]; | ||
| [P in Exclude< | ||
| keyof ElementType<U>, | ||
| keyof ElementType<T> | ||
| >]: ElementType<U>[P]; | ||
| } | ||
| | Record<string, never>; | ||
| | EmptyObject; | ||
|
|
||
| // export type DiffUpdatedType<T, U> = | ||
| // | { | ||
| // [P in keyof T & keyof U]?: DiffUpdatedType<T[P], U[P]>; | ||
| // } | ||
| // | Record<string, never>; | ||
|
|
||
| export type DiffUpdatedType<T, U> = | ||
| | { | ||
| [P in keyof T & keyof U]?: DiffUpdatedType<T[P], U[P]>; | ||
| [P in keyof ElementType<T> & keyof ElementType<U>]?: DiffUpdatedType< | ||
| ElementType<T>[P], | ||
| ElementType<U>[P] | ||
| >; | ||
| } | ||
| | Record<string, never>; | ||
| | U | ||
| | EmptyObject; |
This file contains hidden or 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
117 changes: 0 additions & 117 deletions
test/added.test.js
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
test/added.test.ts
This file contains hidden or 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,128 @@ | ||
| import addedDiff from "../src/added"; | ||
|
|
||
| describe(".addedDiff", () => { | ||
| describe("base case", () => { | ||
| describe("equal", () => { | ||
| test.each([ | ||
| ["int", 1], | ||
| ["string", "a"], | ||
| ["boolean", true], | ||
| ["null", null], | ||
| ["undefined", undefined], | ||
| ["object", { a: 1 }], | ||
| ["array", [1]], | ||
| ["function", () => ({})], | ||
| ["date", new Date()], | ||
| ])( | ||
| "returns empty object when given values of type %s are equal", | ||
| (type, value) => { | ||
| expect(addedDiff(value, value)).toEqual({}); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe("not equal and not object", () => { | ||
| test.each([ | ||
| [1, 2], | ||
| ["a", "b"], | ||
| [true, false], | ||
| ["hello", null], | ||
| ["hello", undefined], | ||
| [null, undefined], | ||
| [undefined, null], | ||
| [null, { a: 1 }], | ||
| ["872983", { areaCode: "+44", number: "872983" }], | ||
| [100, () => ({})], | ||
| [() => ({}), 100], | ||
| [new Date("2017年01月01日"), new Date("2017年01月02日")], | ||
| ])( | ||
| "returns empty object when values are not equal (%s, %s)", | ||
| (lhs, rhs) => { | ||
| expect(addedDiff(lhs, rhs)).toEqual({}); | ||
| } | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("recursive case", () => { | ||
| describe("object", () => { | ||
| test("returns empty object when given objects are updated", () => { | ||
| expect(addedDiff({ a: 1 }, { a: 2 })).toEqual({}); | ||
| }); | ||
|
|
||
| test("returns empty object when right hand side has deletion", () => { | ||
| expect(addedDiff({ a: 1, b: 2 }, { a: 1 })).toEqual({}); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side value when a key value has been added to the root", () => { | ||
| expect(addedDiff({ a: 1 }, { a: 1, b: 2 })).toEqual({ b: 2 }); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side value when a key value has been added deeply", () => { | ||
| expect( | ||
| addedDiff({ a: { b: 1 } }, { a: { b: 1, c: 2 } }) | ||
| ).toEqual({ a: { c: 2 } }); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side with added date", () => { | ||
| expect(addedDiff({}, { date: new Date("2016") })).toEqual({ | ||
| date: new Date("2016"), | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("arrays", () => { | ||
| test("returns empty object when array is updated", () => { | ||
| expect(addedDiff([1], [2])).toEqual({}); | ||
| }); | ||
|
|
||
| test("returns empty object when right hand side array has deletions", () => { | ||
| expect(addedDiff([1, 2, 3], [1, 3])).toEqual({}); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side array as object of indices to value when right hand side array has additions", () => { | ||
| expect(addedDiff([1, 2, 3], [1, 2, 3, 9])).toEqual({ 3: 9 }); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side with added date", () => { | ||
| expect(addedDiff([], [new Date("2016")])).toEqual({ | ||
| 0: new Date("2016"), | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("object create null", () => { | ||
| test("returns subset of right hand side value when a key value has been added to the root", () => { | ||
| const lhs = Object.create(null); | ||
| const rhs = Object.create(null); | ||
| lhs.a = 1; | ||
| rhs.a = 1; | ||
| rhs.b = 2; | ||
| expect(addedDiff(lhs, rhs)).toEqual({ b: 2 }); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side value when a key value has been added deeply", () => { | ||
| const lhs = Object.create(null); | ||
| const rhs = Object.create(null); | ||
| lhs.a = { b: 1 }; | ||
| rhs.a = { b: 1, c: 2 }; | ||
| expect(addedDiff(lhs, rhs)).toEqual({ a: { c: 2 } }); | ||
| }); | ||
|
|
||
| test("returns subset of right hand side with added date", () => { | ||
| const lhs = Object.create(null); | ||
| const rhs = Object.create(null); | ||
| rhs.date = new Date("2016"); | ||
| expect(addedDiff(lhs, rhs)).toEqual({ date: new Date("2016") }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("object with non-function hasOwnProperty property", () => { | ||
| test("can represent the property in diff despite it being part of Object.prototype", () => { | ||
| const lhs = {}; | ||
| const rhs = { hasOwnProperty: true }; | ||
| expect(addedDiff(lhs, rhs)).toEqual({ hasOwnProperty: true }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.