Skip to content
Draft
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
158 changes: 158 additions & 0 deletions lib/utils/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Derived from `src/compat/object/defaults.ts` in es-toolkit
* (https://github.com/toss/es-toolkit).
*
* Copyright (c) 2024 Viva Republica, Inc
* Copyright OpenJS Foundation and other contributors
*
* Parts of the compatibility layer in `es-toolkit/compat` are derived from
* Lodash (https://github.com/lodash/lodash) by the OpenJS Foundation
* (https://openjsf.org/) and Underscore.js by Jeremy Ashkenas, DocumentCloud
* and Investigative Reporters & Editors (http://underscorejs.org/).
*
* This file has been modified from the original source to create a standalone
* adaptation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\d*)$/;

type DefaultSource = object | null | undefined;

function isNil(value: unknown): value is null | undefined {
return value == null;
}

function eq(value: unknown, other: unknown): boolean {
return value === other || (Number.isNaN(value) && Number.isNaN(other));
}

function isLength(value?: unknown): value is number {
return Number.isSafeInteger(value) && (value as number) >= 0;
}

function isArrayLike(value?: unknown): value is { length: number } {
return (
value != null &&
typeof value !== "function" &&
isLength((value as ArrayLike<unknown>).length)
);
}

function isObject(value?: unknown): value is object {
return (
value !== null && (typeof value === "object" || typeof value === "function")
);
}

function isIndex(
value: PropertyKey,
length = Number.MAX_SAFE_INTEGER,
): boolean {
switch (typeof value) {
case "number":
return Number.isInteger(value) && value >= 0 && value < length;
case "symbol":
return false;
case "string":
return IS_UNSIGNED_INTEGER.test(value);
}
}

function isIterateeCall(
value: unknown,
index: unknown,
object: unknown,
): boolean {
if (!isObject(object)) {
return false;
}

if (
(typeof index === "number" &&
isArrayLike(object) &&
isIndex(index) &&
index < object.length) ||
(typeof index === "string" && index in object)
) {
return eq((object as any)[index], value);
}

return false;
}

export function defaults<T, S>(object: T, source: S): NonNullable<S & T>;
export function defaults<T, S1, S2>(
object: T,
source1: S1,
source2: S2,
): NonNullable<S2 & S1 & T>;
export function defaults<T, S1, S2, S3>(
object: T,
source1: S1,
source2: S2,
source3: S3,
): NonNullable<S3 & S2 & S1 & T>;
export function defaults<T, S1, S2, S3, S4>(
object: T,
source1: S1,
source2: S2,
source3: S3,
source4: S4,
): NonNullable<S4 & S3 & S2 & S1 & T>;
export function defaults<T>(object: T): NonNullable<T>;
export function defaults(object: any, ...sources: any[]): any;
export function defaults<T extends object, S extends object>(
object: T,
...sources: DefaultSource[]
): object {
object = Object(object);
const objectProto = Object.prototype;

let length = sources.length;
const guard = length > 2 ? sources[2] : undefined;
if (guard && isIterateeCall(sources[0], sources[1], guard)) {
length = 1;
}

for (let i = 0; i < length; i++) {
if (isNil(sources[i])) {
continue;
}

const source = sources[i] as Record<string, unknown>;
const keys = Object.keys(source);

for (let j = 0; j < keys.length; j++) {
const key = keys[j];
const value = (object as any)[key];

if (
value === undefined ||
(!objectProto.hasOwnProperty.call(object, key) &&
eq(value, objectProto[key as keyof typeof objectProto]))
) {
(object as any)[key] = source[key];
}
}
}

return object;
}
4 changes: 2 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fsPromises } from "fs";
import { resolve } from "path";
import { parse as urllibParse } from "url";
import { defaults, noop } from "./lodash";
import { defaults, isArguments, noop } from "./lodash";
import { Callback } from "../types";
import Debug from "./debug";

Expand Down Expand Up @@ -358,4 +358,4 @@ export async function getPackageMeta() {
}
}

export { Debug, defaults, noop };
export { Debug, defaults, isArguments, noop };
49 changes: 49 additions & 0 deletions lib/utils/isArguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Derived from `src/compat/predicate/isArguments.ts` in es-toolkit
* (https://github.com/toss/es-toolkit).
*
* Copyright (c) 2024 Viva Republica, Inc
* Copyright OpenJS Foundation and other contributors
*
* Parts of the compatibility layer in `es-toolkit/compat` are derived from
* Lodash (https://github.com/lodash/lodash) by the OpenJS Foundation
* (https://openjsf.org/) and Underscore.js by Jeremy Ashkenas, DocumentCloud
* and Investigative Reporters & Editors (http://underscorejs.org/).
*
* This file has been modified from the original source to create a standalone
* adaptation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

function getTag(value: unknown) {
if (value == null) {
return value === undefined ? "[object Undefined]" : "[object Null]";
}

return Object.prototype.toString.call(value);
}

export function isArguments(value?: unknown): value is IArguments {
return (
value !== null &&
typeof value === "object" &&
getTag(value) === "[object Arguments]"
);
}
6 changes: 2 additions & 4 deletions lib/utils/lodash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import defaults = require("lodash.defaults");
import isArguments = require("lodash.isarguments");

export function noop() {}

export { defaults, isArguments };
export * from "./defaults";
export * from "./isArguments";
60 changes: 6 additions & 54 deletions package-lock.json

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

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
"cluster-key-slot": "1.1.1",
"debug": "4.4.3",
"denque": "2.1.0",
"lodash.defaults": "4.2.0",
"lodash.isarguments": "3.1.0",
"redis-errors": "1.2.0",
"redis-parser": "3.0.0",
"standard-as-callback": "2.1.0"
Expand All @@ -61,8 +59,6 @@
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/debug": "^4.1.5",
"@types/lodash.defaults": "^4.2.7",
"@types/lodash.isarguments": "^3.1.7",
"@types/mocha": "^9.1.0",
"@types/node": "^14.18.12",
"@types/redis-errors": "^1.2.1",
Expand Down
Loading
Loading