Skip to content

Commit

Permalink
object-omit -> TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
jednano committed Mar 8, 2019
1 parent 0ca08ba commit a093c6d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/object-omit/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function omit<T extends object, K extends keyof T>(obj: T | null | undefined, remove: keyof T | Array<K | ReadonlyArray<K>>, ...rest: Array<K | ReadonlyArray<K>>): any;

This comment has been minimized.

Copy link
@jednano

jednano Mar 8, 2019

Author Contributor

This file is generated and not meant to be human readable.

export = omit;
9 changes: 4 additions & 5 deletions packages/object-omit/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/object-omit/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/object-omit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*

This comment has been minimized.

Copy link
@jednano

jednano Mar 8, 2019

Author Contributor

I made this a separate file instead of git mv just to illustrate the actual change between the TS-generated JS file and the original JS source file.

var obj = {a: 3, b: 5, c: 9};
omit(obj, ['a', 'c']); // {b: 5}
omit(obj, a, c); // {b: 5}
omit(obj, ['a', 'b', 'd']); // {c: 9}
omit(obj, ['a', 'a']); // {b: 5, c: 9}
*/

function omit<T extends object, K extends keyof T>(
obj: T | null | undefined,
remove: keyof T | Array<K | ReadonlyArray<K>>,
...rest: Array<K | ReadonlyArray<K>>
) {
var result = {} as any;
if (typeof remove === 'string') {
remove = [remove as any].concat(rest)
}
for (var prop in obj) {
if (!obj.hasOwnProperty || obj.hasOwnProperty(prop)) {
if ((remove as Array<K | ReadonlyArray<K>>).indexOf(prop as unknown as K) === -1) {
result[prop as string] = obj[prop];
}
}
}
return result;
}

export = omit;
2 changes: 2 additions & 0 deletions test/object-omit/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check

This comment has been minimized.

Copy link
@jednano

jednano Mar 8, 2019

Author Contributor

This enables me to preserve the existing test file in JS, but still give me editor errors if I try to do something that violates the defined TS types.

var test = require('../util/test')(__filename);
var omit = require('../../packages/object-omit');
var compare = require('../../packages/collection-compare');
Expand Down Expand Up @@ -35,6 +36,7 @@ test('omit using arguments', function(t) {
test('omit using a non-existent key', function(t) {
t.plan(1);
var obj = {a: 3, b: 5, c: 9};
// @ts-ignore

This comment has been minimized.

Copy link
@jednano

jednano Mar 8, 2019

Author Contributor

Here's an example of an (intentional) violation that we want to skip.

t.ok(compare(omit(obj, ['a', 'b', 'd']), {c: 9}));
t.end();
});
Expand Down

0 comments on commit a093c6d

Please sign in to comment.