diff --git a/packages/utilities/package.json b/packages/utilities/package.json index 4caf8f57..79ae2c29 100644 --- a/packages/utilities/package.json +++ b/packages/utilities/package.json @@ -6,13 +6,18 @@ "author": "Andrew Courtice ", "description": "Harlem utilities package", "homepage": "https://harlemjs.com", - "type": "module", - "main": "./dist/index.cjs", - "module": "./dist/index.js", - "exports": "./dist/index.js", - "unpkg": "./dist/index.global.js", - "types": "./dist/index.d.ts", - "source": "./src/index.ts", + "source": "src/index.ts", + "main": "dist/index.js", + "module": "dist/esm/index.js", + "unpkg": "dist/iife/index.js", + "jsdelivr": "dist/iife/index.js", + "types": "dist/index.d.ts", + "exports": { + ".": { + "import": "./dist/esm/index.js", + "require": "./dist/index.js" + } + }, "keywords": [ "vue", "state", @@ -31,5 +36,11 @@ "dev": "tsup --watch src", "build": "tsup", "prepublish": "yarn build" + }, + "peerDependencies": { + "vue": "^3.2.0" + }, + "devDependencies": { + "vue": "^3.2.0" } } diff --git a/packages/utilities/src/object/clone.ts b/packages/utilities/src/object/clone.ts index 3847df3c..9875c324 100644 --- a/packages/utilities/src/object/clone.ts +++ b/packages/utilities/src/object/clone.ts @@ -1,5 +1,10 @@ import getType from '../type/get-type'; +import { + unref, + UnwrapRef +} from 'vue'; + import type { Constructable, RuntimeType, @@ -13,6 +18,13 @@ function cloneBasic(input: Constructable): unknown { return new input.constructor(input); } +function cloneRegex(input: RegExp): RegExp { + const clonedRegex = new RegExp(input.source); + clonedRegex.lastIndex = input.lastIndex; + + return clonedRegex; +} + function cloneSymbol(input: symbol): symbol { return Object(Symbol.prototype.valueOf.call(input)); } @@ -52,15 +64,19 @@ function cloneMap(input: Map): Map { } const CLONE_MAP = { - default: () => null, - null: () => null, - undefined: () => null, - boolean: cloneBasic, - number: cloneBasic, - string: cloneBasic, + default: cloneIdentity, + + // Primitives + null: cloneIdentity, + undefined: cloneIdentity, + boolean: cloneBasic, // only for new Boolean() + number: cloneBasic, // only for new Number() + string: cloneBasic, // only for new String() + + // Objects error: cloneBasic, date: cloneBasic, - regexp: cloneBasic, + regexp: cloneRegex, function: cloneIdentity, symbol: cloneSymbol, array: cloneArray, @@ -69,13 +85,14 @@ const CLONE_MAP = { set: cloneSet, } as Record unknown)>; -export default function clone(value: TValue): TValue { +export default function clone(value: TValue): UnwrapRef { if (typeof value !== 'object' || value === null) { - return value; + return value as UnwrapRef; } const type = getType(value); const cloner = CLONE_MAP[type] || CLONE_MAP.default; + const input = unref(value); - return cloner(value) as TValue; + return cloner(input) as UnwrapRef; } \ No newline at end of file diff --git a/packages/utilities/test/clone.test.ts b/packages/utilities/test/clone.test.ts index 812dd2b1..b20bc47c 100644 --- a/packages/utilities/test/clone.test.ts +++ b/packages/utilities/test/clone.test.ts @@ -1,9 +1,17 @@ import clone from '../src/object/clone'; +import { + isReactive, + isRef, + reactive, + ref, +} from 'vue'; + function getSimpleTypes(): Record { return { num: 1, und: undefined, + str: 'hello world', func1: () => console.log('test'), func2: function (a: number, b: number) { return a + b; @@ -58,6 +66,25 @@ describe('Utilities', () => { } }); + + test('Should unwrap reactive objects', () => { + const source = { + a: ref(4), + b: ref([1, 2, 3]), + c: { + d: reactive({ + e: 1, + }), + }, + }; + + const copy = clone(source); + + expect(isRef(copy.a)).toBe(false); + expect(isRef(copy.b)).toBe(false); + expect(isReactive(copy.c.d)).toBe(false); + }); + }); });