Skip to content

Commit

Permalink
strict mode for types
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Jul 9, 2019
1 parent 9fa9787 commit eb2b37f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
38 changes: 19 additions & 19 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('add', () => {
});

it('should add to the array object directly if an empty path', () => {
const path: null = null;
const path: any = null;
const value = 'value';
const object: any[] = [];

Expand All @@ -101,7 +101,7 @@ describe('add', () => {
});

it('should return the value directly if the object is not an array and path is empty', () => {
const path: null = null;
const path: any = null;
const value = 'value';
const object = {};

Expand Down Expand Up @@ -193,7 +193,7 @@ describe('addWith', () => {

it('should add to the array object directly if an empty path', () => {
const fn = (value: any) => ({ value });
const path: null = null;
const path: any = null;
const object: any[] = [];

const result = addWith(fn, path, object);
Expand All @@ -208,7 +208,7 @@ describe('addWith', () => {

it('should return the value directly if the object is not an array and path is empty', () => {
const fn = (value: any) => ({ value });
const path: null = null;
const path: any = null;
const object = {};

const result = addWith(fn, path, object);
Expand Down Expand Up @@ -667,7 +667,7 @@ describe('assignWith', () => {
});

it('should return the original objects if the path is empty and the fn returns falsy', () => {
const fn = (value: any): object => null;
const fn = (value: any): any => null;
const path: any[] = [];
const object = { untouched: true };

Expand All @@ -677,7 +677,7 @@ describe('assignWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object = {};

Expand Down Expand Up @@ -716,7 +716,7 @@ describe('call', () => {
const parameters: any[] = [123, null];
const context = { iam: 'context' };

const fn = jest.fn().mockImplementation(function () {
const fn = jest.fn().mockImplementation(function (this: any) {
expect(this).toBe(context);

return 'called';
Expand Down Expand Up @@ -864,7 +864,7 @@ describe('callWith', () => {
const parameters: any[] = [123, null];
const context: object = { iam: 'context' };

const fn = jest.fn().mockImplementation(function () {
const fn = jest.fn().mockImplementation(function (this: any) {
expect(this).toBe(context);

return 'called';
Expand Down Expand Up @@ -1062,7 +1062,7 @@ describe('callWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path: any[] = [];
const parameters: any[] = [123, null];
const object = jest.fn().mockReturnValue('called');
Expand Down Expand Up @@ -1366,7 +1366,7 @@ describe('getWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};

Expand Down Expand Up @@ -1528,7 +1528,7 @@ describe('getWithOr', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};
const fallbackValue: string = 'fallback';
Expand Down Expand Up @@ -1749,7 +1749,7 @@ describe('hasWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};

Expand Down Expand Up @@ -2095,7 +2095,7 @@ describe('isWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};
const value: any = 'bar';
Expand Down Expand Up @@ -2555,7 +2555,7 @@ describe('mergeWith', () => {
});

it('should return the original objects if the path is empty and the fn returns falsy', () => {
const fn = (value: any): object => null;
const fn = (value: any): any => null;
const path: any[] = [];
const object: unchanged.Unchangeable = { untouched: true };

Expand All @@ -2565,15 +2565,15 @@ describe('mergeWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};

expect(() => mergeWith(fn, path, object)).toThrowError();
});

it('should return the value returned by fn if the object is not cloneable', () => {
const fn = (value: any): object => null;
const fn = (value: any): any => null;
const path: any[] = [];
const value: any = { foo: 'bar' };
const object: any = 123;
Expand Down Expand Up @@ -2922,7 +2922,7 @@ describe('notWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};
const value: any = 'bar';
Expand Down Expand Up @@ -3186,7 +3186,7 @@ describe('removeWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};

Expand Down Expand Up @@ -3411,7 +3411,7 @@ describe('setWith', () => {
});

it('should throw if the function passed is not a function', () => {
const fn: null = null;
const fn: any = null;
const path = ['foo'];
const object: unchanged.Unchangeable = {};

Expand Down
18 changes: 11 additions & 7 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('getDeepClone', () => {
const value = 'value';

const path = 'deeply[0].nested';
const object: null = null;
const object: any = null;
const callback = jest.fn().mockImplementation((ref: unchanged.Unchangeable, key: string) => {
expect(ref).toEqual({});
expect(key).toEqual(path.split('.')[1]);
Expand Down Expand Up @@ -278,10 +278,10 @@ describe('getFullPath', () => {
expect(result).toEqual(path);
});

it('should return the added index if path is an empty string and value is an array', () => {
it('should return the added index if path is null and value is an array', () => {
const path: any = null;
const object: any[] = [];
const fn: void = undefined;
const fn: any = undefined;

const result = getFullPath(path, object, fn);

Expand Down Expand Up @@ -348,7 +348,9 @@ describe('getOwnProperties', () => {

const result = getOwnProperties(object);

expect(result).toEqual([].concat(Object.keys(object), [symbol]));
const keys = Object.keys(object);

expect(result).toEqual(keys.concat([(symbol as unknown) as string]));
});

it('should get only keys if no symbols in the object passed exist', () => {
Expand Down Expand Up @@ -376,7 +378,9 @@ describe('getOwnProperties', () => {

const result = getOwnProperties(object);

expect(result).toEqual([].concat(Object.keys(object), [symbol]));
const keys = Object.keys(object);

expect(result).toEqual(keys.concat([(symbol as unknown) as string]));
});
});

Expand Down Expand Up @@ -646,7 +650,7 @@ describe('getValueAtPath', () => {

it('should return undefined when the object does not exist', () => {
const path = 'path';
const object: null = null;
const object: any = null;
const fallbackValue: undefined = undefined;

const result: void = getValueAtPath(path, object, fallbackValue);
Expand All @@ -656,7 +660,7 @@ describe('getValueAtPath', () => {

it('should return the fallback when the object does not exist and a fallback is provided', () => {
const path = 'path';
const object: null = null;
const object: any = null;
const fallbackValue = 'fallback';

const result: void = getValueAtPath(path, object, fallbackValue);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@
"lint": "NODE_ENV=test tslint 'src/*.ts'",
"lint:fix": "npm run lint -- --fix",
"prepublish": "if in-publish; then npm run prepublish:compile; fi",
"prepublish:compile": "npm run lint && npm run test:coverage && npm run dist",
"prepublish:compile": "npm run lint && npm run typecheck && npm run test:coverage && npm run dist",
"start": "npm run dev",
"test": "NODE_PATH=. jest",
"test:coverage": "npm run test -- --coverage",
"test:watch": "npm run test -- --watch"
"test:watch": "npm run test -- --watch",
"typecheck": "tsc --noEmit"
},
"sideEffects": false,
"types": "dist/index.d.ts",
Expand Down
11 changes: 6 additions & 5 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export function createMerge<IsWith extends true | false>(
const result: unchanged.Unchangeable = getDeepClone(
path,
object,
(ref: unchanged.Unchangeable, key: string): void => {
(ref: unchanged.Unchangeable, key: unchanged.PathItem): void => {
const objectToMerge: any = fn(ref[key], ...extraArgs);

if (objectToMerge) {
Expand Down Expand Up @@ -317,7 +317,7 @@ export function createMerge<IsWith extends true | false>(
: getDeepClone(
path,
object,
(ref: unchanged.Unchangeable, key: string): void => {
(ref: unchanged.Unchangeable, key: unchanged.PathItem): void => {
ref[key] = getMergedObject(ref[key], objectToMerge, isDeep);
},
);
Expand All @@ -341,7 +341,7 @@ export function createNot<IsWith extends true | false>(
): unchanged.NotWith | unchanged.Not {
const is: Function = createIs(isWithHandler);

return function not() {
return function not(this: any) {
return !is.apply(this, arguments);
};
}
Expand Down Expand Up @@ -453,7 +453,7 @@ export function createSet<IsWith extends true | false>(
: getDeepClone(
path,
object,
(ref: unchanged.Unchangeable, key: string): void => {
(ref: unchanged.Unchangeable, key: unchanged.PathItem): void => {
ref[key] = fn(ref[key], ...extraArgs);
},
);
Expand All @@ -470,7 +470,7 @@ export function createSet<IsWith extends true | false>(
: getDeepClone(
path,
object,
(ref: unchanged.Unchangeable, key: string): void => {
(ref: unchanged.Unchangeable, key: unchanged.PathItem): void => {
ref[key] = value;
},
);
Expand All @@ -496,6 +496,7 @@ export function createAdd<IsWith extends true | false>(

if (isWithHandler) {
return function addWith(
this: any,
fn: unchanged.WithHandler,
path: unchanged.Path,
object: unchanged.Unchangeable,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"noImplicitAny": true,
"outDir": "./dist",
"sourceMap": true,
"strict": true,
"target": "es5",
"types": ["jest", "node", "react"]
},
Expand Down

0 comments on commit eb2b37f

Please sign in to comment.