Skip to content
Merged
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Changelog

### [v2.1.0](https://github.com/panates/jsopen-objects/compare/v2.0.2...v2.1.0) -
### [v2.1.1](https://github.com/panates/jsopen-objects/compare/v2.1.0...v2.1.1) -

#### 🚀 New Features
#### 🪲 Fixes

- feat: Added `updateErrorMessage` helper method @Eray Hanoğlu
- fix: Fixed "keepExisting" with "deep" option do not overwrite existing @Eray Hanoğlu
4 changes: 2 additions & 2 deletions package-lock.json

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

37 changes: 9 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jsopen/objects",
"description": "Helper utilities for working with JavaScript objects and arrays",
"version": "2.1.0",
"version": "2.1.1",
"author": "Panates",
"license": "MIT",
"private": true,
Expand Down Expand Up @@ -32,15 +32,12 @@
},
"scripts": {
"compile": "tsc --noEmit",
"prebuild": "npm run clean:build && npm run lint",
"build": "tsc -b tsconfig-build.json",
"postbuild": "node ./support/postbuild.cjs && rimraf build/*.tsbuildinfo",
"clean": "npm run clean:src && npm run clean:dist",
"clean:dist": "rimraf build coverage",
"clean:build": "rimraf build *.tsbuildinfo",
"clean:src": "ts-cleanup -s src --all | ts-cleanup -s test",
"prebuild": "npm run clean:dist && npm run lint",
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "tsc -b tsconfig-build-cjs.json && cp support/package.cjs.json ./build/cjs/package.json",
"build:esm": "tsc -b tsconfig-build-esm.json && cp support/package.esm.json ./build/esm/package.json",
"postbuild": "npm run postbuild:copyfiles && node ./support/postbuild.cjs",
"postbuild:copyfiles": "cp LICENSE README.md CHANGELOG.md ./build",
"lint": "eslint . --max-warnings=0",
"lint:fix": "eslint . --max-warnings=0 --fix",
"format": "prettier . --write --log-level=warn",
Expand All @@ -52,23 +49,15 @@
"version": "auto-changelog -p --starting-version v4.0.0 && git add CHANGELOG.md"
},
"type": "module",
"module": "./index.js",
"types": "./index.d.ts",
"exports": {
".": {
"import": {
"types": "./types/index.d.ts",
"default": "./esm/index.js"
},
"require": {
"types": "./types/index.d.cts",
"default": "./cjs/index.js"
},
"default": "./esm/index.js"
"types": "./index.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"main": "./cjs/index.js",
"module": "./esm/index.js",
"types": "./types/index.d.ts",
"contributors": [
"Eray Hanoglu <e.hanoglu@panates.com>"
],
Expand All @@ -79,14 +68,6 @@
"engines": {
"node": ">= 16.0"
},
"files": [
"cjs/",
"esm/",
"types/",
"LICENSE",
"README.md",
"CHANGELOG.md"
],
"keywords": [
"object",
"util",
Expand Down
101 changes: 50 additions & 51 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,24 @@ export function merge(
) {
continue;
}
_goDeep = !!(
deep &&
typeof srcVal === 'object' &&
(!isBuiltIn(srcVal) || Array.isArray(srcVal))
);
if (_goDeep) {
if (deepFn)
_goDeep = deepFn(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
});
else
_goDeep = deepFull || isPlainObject(srcVal) || Array.isArray(srcVal);
}

if (keepExisting && hasOwnProperty.call(target, key)) {
if (!_goDeep && keepExisting && hasOwnProperty.call(target, key)) {
if (!keepExistingFn) continue;
if (
keepExistingFn(srcVal, {
Expand Down Expand Up @@ -145,59 +161,42 @@ export function merge(
continue;
}

if (
deep &&
typeof srcVal === 'object' &&
(!isBuiltIn(srcVal) || Array.isArray(srcVal))
) {
_goDeep =
(deepFn &&
deepFn(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
})) ||
(!deepFn &&
(deepFull || isPlainObject(srcVal) || Array.isArray(srcVal)));
if (_goDeep) {
/** Array */
if (Array.isArray(srcVal)) {
if (
Array.isArray(target[key]) &&
(mergeArrays ||
mergeArraysFn?.(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
}))
) {
target[key] = _arrayClone(
target[key],
parentPath + (parentPath ? '.' : '') + String(key),
);
} else target[key] = [];

target[key].push(
..._arrayClone(
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
),
);
if (mergeArraysUnique)
target[key] = Array.from(new Set(target[key]));
continue;
} else {
/** Object */
if (!isObject(target[key])) target[key] = {};
_merge(
if (_goDeep) {
/** Array */
if (Array.isArray(srcVal)) {
if (
Array.isArray(target[key]) &&
(mergeArrays ||
mergeArraysFn?.(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
}))
) {
target[key] = _arrayClone(
target[key],
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
);
continue;
}
} else target[key] = [];

target[key].push(
..._arrayClone(
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
),
);
if (mergeArraysUnique) target[key] = Array.from(new Set(target[key]));
continue;
} else {
/** Object */
if (!isObject(target[key])) target[key] = {};
_merge(
target[key],
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
);
continue;
}
}

Expand Down
3 changes: 0 additions & 3 deletions support/package.cjs.json

This file was deleted.

3 changes: 0 additions & 3 deletions support/package.esm.json

This file was deleted.

31 changes: 23 additions & 8 deletions support/postbuild.cjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
const fs = require('node:fs');
const path = require('node:path');

function clearPackageJson() {
const targetPath = path.resolve(__dirname, '../build');

function postBuild() {
const projectRoot = process.cwd();
const json = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../package.json'), 'utf-8'),
fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf-8'),
);

const buildDir = path.join(projectRoot, 'build');
if (!fs.existsSync(buildDir)) throw new Error('Build directory not found');

json.type = 'module';
delete json.private;
delete json.scripts;
delete json.devDependencies;

fs.writeFileSync(
path.resolve(targetPath, 'package.json'),
path.resolve(buildDir, 'package.json'),
JSON.stringify(json, undefined, 2),
'utf-8',
);
fs.copyFileSync(
path.resolve(targetPath, './types/index.d.ts'),
path.resolve(targetPath, './types/index.d.cts'),
path.resolve('./README.md'),
path.resolve(buildDir, 'README.md'),
);
fs.copyFileSync(path.resolve('./LICENSE'), path.resolve(buildDir, 'LICENSE'));

/** Update version */
const constantsFile = path.resolve(buildDir, 'constants.js');
if (fs.existsSync(constantsFile)) {
let content = fs.readFileSync(constantsFile, 'utf8');
content = content.replace(`version = '1'`, `version = '${json.version}'`);
fs.writeFileSync(constantsFile, content, 'utf-8');
}
}

clearPackageJson();
postBuild();
7 changes: 6 additions & 1 deletion tsconfig-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"extends": "@panates/tsconfig/node.json",
"compilerOptions": {
"types": ["node"],
"esModuleInterop": true
"sourceMap": true,
"moduleResolution": "nodenext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "NodeNext",
"target": "ESNext"
}
}
10 changes: 0 additions & 10 deletions tsconfig-build-cjs.json

This file was deleted.

3 changes: 1 addition & 2 deletions tsconfig-build-esm.json → tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"outDir": "build/esm",
"declarationDir": "build/types"
"outDir": "build",
},
"include": ["src"]
}
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"types": ["node"],
"module": "NodeNext",
"moduleResolution": "NodeNext"
"types": ["node"]
},
"include": ["src"],
"exclude": ["node_modules", "build"]
Expand Down