Skip to content

Commit

Permalink
[H-0003] [devx] Setup pre-commit and pre-push hooks (#9)
Browse files Browse the repository at this point in the history
* [H-0003] Installed Husky for pre-commit hooks

* [H-0003] Minor improvements to the code

* [H-0003] Update version

Co-authored-by: Kovalenkov Pavel <p.kovalenkov@cian.ru>
  • Loading branch information
kovalenkovpu and Kovalenkov Pavel committed Jan 24, 2023
1 parent abdfc30 commit 7f9b615
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run precommit
4 changes: 1 addition & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ export default {
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
testPathIgnorePatterns: ["/node_modules/", "/lib/", "/coverage/"],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
Expand Down
38 changes: 30 additions & 8 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handilities",
"version": "0.0.3",
"version": "0.0.4",
"description": "Handy utilities for JS, TS, React in browser and server env",
"main": "lib",
"scripts": {
Expand All @@ -10,7 +10,9 @@
"prettier:check": "prettier --check \"./src/**/*.{js,ts}\"",
"prettier:write": "prettier --write \"./src/**/*.{js,ts}\"",
"eslint": "eslint --ext .js,.ts .",
"lint": "npm run prettier:check && npm run eslint"
"lint": "npm run prettier:check && npm run eslint",
"precommit": "npm run lint && jest --coverage=false && tsc --noEmit",
"prepare": "husky install"
},
"author": "Pavel Kovalenkov",
"keywords": [
Expand All @@ -31,7 +33,8 @@
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"husky": "^8.0.0"
},
"files": [
"lib/**/*"
Expand Down
7 changes: 5 additions & 2 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from "./removeDeepByKey";
export * from "./removeByKey";
export { removeDeepByKey } from "./removeDeepByKey";
export type { IRemoveDeepByKey } from "./removeDeepByKey";

export { removeByKey } from "./removeByKey";
export type { IRemoveByKey } from "./removeByKey";
8 changes: 5 additions & 3 deletions src/common/internal/deleteKeyInObjectByPath.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { deleteSingleKeyInObject } from "./deleteSingleKeyInObject";

export function deleteKeyInObjectByPath<T extends Record<string, unknown>, K extends keyof T>(
function deleteKeyInObjectByPath<T extends Record<string, unknown>, K extends keyof T>(
path: [K],
target: T,
): Omit<T, K>;

export function deleteKeyInObjectByPath<R extends Record<string, unknown>>(
function deleteKeyInObjectByPath<R extends Record<string, unknown>>(
path: string[],
target: Record<string, unknown>,
): R;

export function deleteKeyInObjectByPath<R extends Record<string, unknown>>(
function deleteKeyInObjectByPath<R extends Record<string, unknown>>(
path: string[],
target: Record<string, unknown>,
): typeof target | R {
Expand All @@ -31,3 +31,5 @@ export function deleteKeyInObjectByPath<R extends Record<string, unknown>>(
[key]: deleteKeyInObjectByPath(rest, newTarget),
};
}

export { deleteKeyInObjectByPath };
4 changes: 3 additions & 1 deletion src/common/internal/deleteSingleKeyInObject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export function deleteSingleKeyInObject<T extends Record<string, unknown>, K extends keyof T>(
function deleteSingleKeyInObject<T extends Record<string, unknown>, K extends keyof T>(
key: K,
target: T,
): Omit<T, K> {
const { [key]: _, ...rest } = target;

return rest;
}

export { deleteSingleKeyInObject };
9 changes: 5 additions & 4 deletions src/common/internal/getByPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ interface IGetByPath {
(path: string | string[], target: Record<string, unknown>): unknown;
}

export const getByPath: IGetByPath = (path, target) => {
const getByPath: IGetByPath = (path, target) => {
if (!path.length) {
return target;
}
Expand All @@ -12,11 +12,12 @@ export const getByPath: IGetByPath = (path, target) => {
}

if (Array.isArray(path)) {
const pathCopy = [...path];
const [currentKey] = pathCopy;
const [currentKey] = path;

return getByPath(pathCopy.splice(1), target[currentKey] as Record<string, unknown>);
return getByPath(path.splice(1), target[currentKey] as Record<string, unknown>);
} else {
return target[path];
}
};

export { getByPath };
4 changes: 3 additions & 1 deletion src/common/internal/objectHasKeysByPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function objectHasKeysByPath(path: string[], target: Record<string, unknown>): boolean {
function objectHasKeysByPath(path: string[], target: Record<string, unknown>): boolean {
let objectHasKeys = true;
let targetByKey: Record<string, unknown> = target;

Expand All @@ -16,3 +16,5 @@ export function objectHasKeysByPath(path: string[], target: Record<string, unkno

return objectHasKeys;
}

export { objectHasKeysByPath };
9 changes: 8 additions & 1 deletion src/common/removeByKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { deleteKeyInObjectByPath, deleteSingleKeyInObject, objectHasKeysByPath }

type TPath = string | string[];

interface IRemoveByKey {
(path: TPath, target: Record<string, unknown>): Record<string, unknown>;
}

/**
* Removes key-value pair in the object by a provided "path"
* @param path array containing path to the given key of the object, e.g. ['a', 'b']
* @param target object to remove the key-value pair from, e.g. { a: { b: 'some value' } }
* @returns new object with deleted key-value pair, e.g. { a: { } }
*/
export const removeByKey = <T extends Record<string, unknown>>(path: TPath, target: T) => {
const removeByKey: IRemoveByKey = (path, target) => {
if (!path.length) {
return target;
}
Expand All @@ -23,3 +27,6 @@ export const removeByKey = <T extends Record<string, unknown>>(path: TPath, targ

return deleteSingleKeyInObject(path, target);
};

export type { IRemoveByKey };
export { removeByKey };
7 changes: 5 additions & 2 deletions src/common/removeDeepByKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getByPath, removeByKey } from "./internal";

export interface IRemoveDeepByKey {
interface IRemoveDeepByKey {
(path: string[], target: Record<string, unknown>): Record<string, unknown>;
}

Expand All @@ -11,7 +11,7 @@ export interface IRemoveDeepByKey {
* @param target object to remove the key-value pair from, e.g. { a: { b: 'some value' } }
* @returns new object with deleted key-value pair, e.g. { }
*/
export const removeDeepByKey: IRemoveDeepByKey = (path, target) => {
const removeDeepByKey: IRemoveDeepByKey = (path, target) => {
const pathCopy = [...path];
const result = removeByKey(pathCopy, target);

Expand All @@ -25,3 +25,6 @@ export const removeDeepByKey: IRemoveDeepByKey = (path, target) => {

return removeDeepByKey(pathCopy, result);
};

export type { IRemoveDeepByKey };
export { removeDeepByKey };
8 changes: 4 additions & 4 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./keyOf";
export * from "./valueOf";
export * from "./objectKeys";
export * from "./objectValues";
export type { KeyOf } from "./keyOf";
export type { ValueOf } from "./valueOf";
export { objectKeys } from "./objectKeys";
export { objectValues } from "./objectValues";
4 changes: 3 additions & 1 deletion src/ts/objectKeys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { KeyOf } from "./keyOf";

export const objectKeys = <T extends Record<string, unknown>>(target: T) => {
const objectKeys = <T extends Record<string, unknown>>(target: T) => {
return Object.keys(target) as KeyOf<T>[];
};

export { objectKeys };
4 changes: 3 additions & 1 deletion src/ts/objectValues.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ValueOf } from "./valueOf";

export const objectValues = <T extends Record<string, unknown>>(target: T) => {
const objectValues = <T extends Record<string, unknown>>(target: T) => {
return Object.values(target) as ValueOf<T>[];
};

export { objectValues };

0 comments on commit 7f9b615

Please sign in to comment.