Skip to content

Commit

Permalink
Adding omit function
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaal111 committed Apr 21, 2024
1 parent bd81bff commit a650613
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 28 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ module.exports = {
es2021: true,
node: true,
},
extends: ['standard-with-typescript'],
extends: ['standard-with-typescript', 'prettier'],
parserOptions: {
project: './tsconfig.json',
},
plugins: ['@stylistic/js'],
root: true,
rules: {
'@typescript-eslint/semi': 'off',
'@typescript-eslint/indent': 'off',
'@stylistic/js/semi': ['error', 'always'],
'@stylistic/js/indent': 'off',
},
overrides: [
{
files: ['./tests/*'],
files: ['./tests/**/*.ts'],
parserOptions: {
project: './tsconfig.tests.json',
},
Expand Down
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2
}
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
"scripts": {
"build": "tsc",
"test": "jest",
"lint": "eslint src/index.ts src/**/*.ts tests/*",
"lint": "eslint src/index.ts src/**/*.ts tests/**/*.ts",
"prepare": "husky",
"prepublish": "rm -rf dist && yarn build"
},
"peerDependencies": {},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"name": "kamaal",
"author": "Kamaal Farah",
"devDependencies": {
Expand All @@ -36,12 +30,14 @@
"@typescript-eslint/parser": "^7.7.0",
"babel-jest": "^29.7.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-promise": "^6.0.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "~5.3"
},
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './module1';
export * from './module2';
export * as objects from './objects';
5 changes: 0 additions & 5 deletions src/module1/index.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/module2/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/objects/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './omit';
17 changes: 17 additions & 0 deletions src/objects/omit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const omit = <
TargetObject extends object,
Fields extends Exclude<keyof TargetObject, symbol | number>,
>(
object: TargetObject,
fieldsToOmit: Fields
): Omit<TargetObject, typeof fieldsToOmit> => {
return Object.entries(object).reduce((acc, [key, value]) => {
if (fieldsToOmit.includes(key)) {
return acc;
}

return { ...acc, [key]: value };
}, {}) as Omit<TargetObject, typeof fieldsToOmit>;
};

export default omit;
7 changes: 0 additions & 7 deletions tests/blah.test.ts

This file was deleted.

13 changes: 13 additions & 0 deletions tests/objects/omit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { omit } from '../../src/objects';

describe('omit', () => {
it('omits', () => {
expect(omit({ yes: true, no: false }, 'yes')).toEqual({ no: false });
});

it('does not omit non existent fields', () => {
const originalObject = { yes: true, no: false };
// @ts-expect-error using an non existent property for testing
expect(omit(originalObject, 'maybe')).toEqual(originalObject);
});
});
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,11 @@ eslint-compat-utils@^0.5.0:
dependencies:
semver "^7.5.4"

eslint-config-prettier@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz#31af3d94578645966c082fcb71a5846d3c94867f"
integrity sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==

eslint-config-standard-with-typescript@^43.0.1:
version "43.0.1"
resolved "https://registry.yarnpkg.com/eslint-config-standard-with-typescript/-/eslint-config-standard-with-typescript-43.0.1.tgz#977862d7d41b0e1f27f399137bbf7b2e017037ff"
Expand Down Expand Up @@ -3895,6 +3900,11 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

prettier@^3.2.5:
version "3.2.5"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368"
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==

pretty-format@^29.0.0, pretty-format@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812"
Expand Down

0 comments on commit a650613

Please sign in to comment.