Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"presets": [["@babel/preset-env", { "targets": "defaults" }]]
"presets": [
["@babel/preset-env", { "targets": "defaults" }],
"@babel/preset-typescript"
]
}
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ jobs:
- run: npm ci
- run: npm run build
- run: npm run test
- run: npm run type-check
- run: npm run lint
- run: npm run format-check
86 changes: 0 additions & 86 deletions docs/test/index.html

This file was deleted.

56 changes: 47 additions & 9 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"browser": "dist/jsondiffpatch.umd.js",
"main": "dist/jsondiffpatch.cjs.js",
"module": "dist/jsondiffpatch.esm.js",
"types": "./dist/index",
"types": "dist/index.d.ts",
"files": [
"dist",
"bin"
Expand All @@ -23,6 +23,7 @@
"test": "jest --coverage",
"format": "prettier . --write",
"format-check": "prettier . --check",
"type-check": "tsc",
"prepack": "npm run build",
"prepublishOnly": "npm run test && npm run lint"
},
Expand All @@ -43,6 +44,7 @@
"@babel/core": "^7.22.10",
"@babel/eslint-parser": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"@babel/preset-typescript": "^7.22.11",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.0",
Expand All @@ -54,7 +56,8 @@
"jest": "^29.6.2",
"prettier": "^3.0.2",
"rollup": "^3.28.0",
"rollup-plugin-visualizer": "^5.9.2"
"rollup-plugin-visualizer": "^5.9.2",
"typescript": "~5.1.6"
},
"license": "MIT",
"engines": {
Expand Down
20 changes: 11 additions & 9 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { visualizer } from 'rollup-plugin-visualizer';

import pkg from './package.json' assert { type: 'json' };

const copySrcFileToDist = copyFromFolderToDist('src');
const copyDocsFileToDist = copyFromFolderToDist('docs');

export default [
{
input: 'src/main.js',
input: 'src/index.ts',
external: ['chalk'],
output: {
name: pkg.name,
Expand All @@ -32,13 +31,14 @@ export default [
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts'],
}),
resolve(), // so Rollup can find node modules
commonjs(), // so Rollup can convert node modules to ES modules
resolve({ extensions: ['.mjs', '.js', '.json', '.node', '.ts'] }),
commonjs(),
],
},
{
input: 'src/main.js',
input: 'src/index.ts',
external: ['chalk', 'diff-match-patch'],
output: {
name: pkg.name,
Expand All @@ -61,13 +61,14 @@ export default [
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts'],
}),
resolve(), // so Rollup can find node modules
commonjs(), // so Rollup can convert node modules to ES modules
resolve({ extensions: ['.mjs', '.js', '.json', '.node', '.ts'] }),
commonjs(),
],
},
{
input: 'src/main.js',
input: 'src/index.ts',
external: [
// external node modules
'diff-match-patch',
Expand All @@ -77,8 +78,9 @@ export default [
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts'],
}),
copySrcFileToDist('index.d.ts'),
resolve({ extensions: ['.mjs', '.js', '.json', '.node', '.ts'] }),
copyDocsFileToDist('formatters-styles/annotated.css'),
copyDocsFileToDist('formatters-styles/html.css'),
],
Expand Down
17 changes: 7 additions & 10 deletions src/clone.js → src/clone.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
const isArray =
typeof Array.isArray === 'function'
? Array.isArray
: (a) => a instanceof Array;

function cloneRegExp(re) {
const regexMatch = /^\/(.*)\/([gimyu]*)$/.exec(re.toString());
function cloneRegExp(re: RegExp) {
const regexMatch = /^\/(.*)\/([gimyu]*)$/.exec(re.toString())!;
return new RegExp(regexMatch[1], regexMatch[2]);
}

export default function clone(arg) {
export default function clone(arg: unknown): unknown {
if (typeof arg !== 'object') {
return arg;
}
if (arg === null) {
return null;
}
if (isArray(arg)) {
if (Array.isArray(arg)) {
return arg.map(clone);
}
if (arg instanceof Date) {
Expand All @@ -27,7 +22,9 @@ export default function clone(arg) {
const cloned = {};
for (const name in arg) {
if (Object.prototype.hasOwnProperty.call(arg, name)) {
cloned[name] = clone(arg[name]);
(cloned as { [name: string]: unknown })[name] = clone(
(arg as { [name: string]: unknown })[name],
);
}
}
return cloned;
Expand Down
24 changes: 20 additions & 4 deletions src/contexts/context.js → src/contexts/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import Pipe from '../pipe';
import { Options } from '../processor';

export default class Context {
setResult(result) {
export default class Context<TResult> {
result?: TResult;
hasResult?: boolean;
exiting?: boolean;
nextPipe?: string | Pipe<this>;
parent?: this;
childName?: string | number;
root?: this;
options?: Options;
children?: this[];
nextAfterChildren?: this | null;
next?: this | null;
pipe?: string;

setResult(result: TResult) {
this.result = result;
this.hasResult = true;
return this;
Expand All @@ -12,7 +26,9 @@ export default class Context {
return this;
}

switchTo(next, pipe) {
switchTo(next: string | Pipe<this>): this;
switchTo(next: this, pipe?: string | Pipe<this>): this;
switchTo(next: string | Pipe<this> | this, pipe?: string | Pipe<this>) {
if (typeof next === 'string' || next instanceof Pipe) {
this.nextPipe = next;
} else {
Expand All @@ -24,7 +40,7 @@ export default class Context {
return this;
}

push(child, name) {
push(child: this, name: string | number) {
child.parent = this;
if (typeof name !== 'undefined') {
child.childName = name;
Expand Down
Loading