Skip to content
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

Convert to TypeScript and ensure strict type safety #101

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended" // Use recommended rules from the @typescript-eslint/eslint-plugin
],
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
"sourceType": "module",
"project": "./tsconfig.json" // Specify it only if you want to use TypeScript specific linting rules
},
"rules": {
}
"plugins": [
"@typescript-eslint" // Use the TypeScript plugin
],
"rules": {}
}
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
coverage
npm-debug.log
yarn-error.log
dist-es
15 changes: 0 additions & 15 deletions index.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
};
6,435 changes: 6,435 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

106 changes: 50 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
{
"name": "deep-object-diff",
"version": "1.1.9",
"description": "Deep diffs two objects, including nested structures of arrays and objects, and return the difference.",
"main": "cjs/index.js",
"module": "mjs/index.js",
"exports": {
".": {
"import": "./mjs/index.js",
"require": "./cjs/index.js",
"types": "./index.d.ts"
}
},
"types": "./index.d.ts",
"scripts": {
"build": "rm -rf dist && babel src -d dist/cjs && node scripts/build.mjs",
"prepublish": "yarn build",
"lint": "eslint src",
"test": "jest",
"test:coverage": "yarn test --coverage",
"test:watch": "yarn test -- --watch"
},
"author": "Matt Phillips",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.16.8",
"@babel/core": "^7.16.12",
"@babel/preset-env": "^7.16.11",
"babel-jest": "^27.4.6",
"eslint": "^8.7.0",
"jest": "^27.4.7"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "12"
}
"name": "deep-object-diff",
"version": "1.2.0",
"description": "Deep diffs two objects, including nested structures of arrays and objects, and return the difference.",
"main": "./dist/cjs/index.js",
"module": "./dist/mjs/index.js",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/mjs/index.d.ts",
"default": "./dist/mjs/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
]
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/mattphillips/deep-object-diff.git"
},
"keywords": [
"diff",
"object",
"deep",
"difference"
],
"bugs": {
"url": "https://github.com/mattphillips/deep-object-diff/issues"
},
"homepage": "https://github.com/mattphillips/deep-object-diff#readme"
},
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.mjs.json && node ./scripts/build.js",
"prepare": "npm run build",
"lint": "eslint src",
"test": "jest",
"test:coverage": "npm test -- --coverage",
"test:watch": "npm test -- --watch"
},
"author": "Matt Phillips",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.7.0",
"jest": "^29.1.2",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mattphillips/deep-object-diff.git"
},
"keywords": [
"diff",
"object",
"deep",
"difference"
],
"bugs": {
"url": "https://github.com/mattphillips/deep-object-diff/issues"
},
"homepage": "https://github.com/mattphillips/deep-object-diff#readme"
}
36 changes: 36 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as path from "path";
import * as fs from "fs";

const DIST = "dist";
const MJS = "mjs";
const CJS = "cjs";
const PKG = "package.json";
const FILES = ["README.md", "LICENSE"];

fs.writeFileSync(
path.join(DIST, CJS, PKG),
JSON.stringify({ type: "commonjs" }, null, 2)
);
fs.writeFileSync(
path.join(DIST, MJS, PKG),
JSON.stringify({ type: "module" }, null, 2)
);

// Inside the MJS folder we need to replace all import x from "y" with import x
// from "y.js"
fs.readdirSync(path.join(DIST, MJS)).forEach((file) => {
const content = fs
.readFileSync(path.join(DIST, MJS, file), "utf-8")
.replace(/from "([^"]+)"/g, 'from "$1.js"');
fs.writeFileSync(path.join(DIST, MJS, file), content);
});

const pkg = fs.readFileSync(PKG, "utf-8");
const json = JSON.parse(pkg);

delete json.scripts;
delete json.devDependencies;

fs.writeFileSync(path.join(DIST, PKG), JSON.stringify(json, null, 2));

FILES.forEach((file) => fs.copyFileSync(file, path.join(DIST, file)));
26 changes: 0 additions & 26 deletions scripts/build.mjs

This file was deleted.

23 changes: 0 additions & 23 deletions src/added.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/added.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { DiffAddedType } from "./types";
import {
isEmpty,
isObject,
hasOwnProperty,
makeObjectWithoutPrototype,
} from "./utils";

const addedDiff = <T, U>(lhs: T, rhs: U): DiffAddedType<T, U> => {
if (
(lhs as unknown) === (rhs as unknown) ||
!isObject(lhs) ||
!isObject(rhs)
)
return {} as DiffAddedType<T, U>;

return Object.keys(rhs).reduce((acc, key) => {
if (hasOwnProperty(lhs, key)) {
const difference = addedDiff(
(lhs as Record<string, unknown>)[key],
(rhs as Record<string, unknown>)[key]
);

if (isObject(difference) && isEmpty(difference)) return acc;

acc[key] = difference;
return acc;
}

acc[key] = (rhs as Record<string, unknown>)[key];
return acc;
}, makeObjectWithoutPrototype()) as DiffAddedType<T, U>;
};

export default addedDiff;
53 changes: 0 additions & 53 deletions src/arrayDiff.js

This file was deleted.