@aoao-y33/utils 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare const isObject: (obj: any) => boolean;
2
+ export declare const mergeDeep: <T>(source: T, target: T | Partial<T>) => T;
3
+ //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ,GAAI,KAAK,GAAG,KAAG,OAEnC,CAAA;AAGD,eAAO,MAAM,SAAS,GAAI,CAAC,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,KAAG,CAWhE,CAAA"}
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@aoao-y33/utils",
3
+ "version": "0.0.1",
4
+ "private": false,
5
+ "description": "js工具包",
6
+ "main": "index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "vite build",
17
+ "add": "pnpm install",
18
+ "publish": "npm publish --access public"
19
+ },
20
+ "keywords": [],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "packageManager": "pnpm@10.32.1",
24
+ "dependencies": {}
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './object'
package/src/object.ts ADDED
@@ -0,0 +1,19 @@
1
+ import {merge, mergeWith} from 'lodash';
2
+ //判断是否是有效对象
3
+ export const isObject = (obj: any): boolean => {
4
+ return ![null, undefined].includes(obj) && typeof obj === 'object' && !Array.isArray(obj);
5
+ }
6
+
7
+ //深度合并两个对象
8
+ export const mergeDeep = <T>(source: T, target: T | Partial<T>): T => {
9
+ if (!isObject(target)) {
10
+ return target as T;
11
+ }
12
+ return mergeWith(source, target, (a, b) => {
13
+ if (!isObject(a)) {
14
+ return b;
15
+ }
16
+ return merge(a, b);
17
+ })
18
+
19
+ }
@@ -0,0 +1,56 @@
1
+ import {isObject, mergeDeep} from "../src";
2
+ import {describe} from "vitest";
3
+
4
+ describe('isObject 测试', () => {
5
+ it('传入普通对象应该返回 true', () => {
6
+ expect(isObject({name: 'ax-y33'})).toBe(true)
7
+ })
8
+
9
+ it('传入数组应该返回 true', () => {
10
+ expect(isObject([1, 2, 3])).toBe(true)
11
+ })
12
+
13
+ it('传入字符串或数字应该返回 false', () => {
14
+ expect(isObject('hello')).toBe(false)
15
+ expect(isObject(123)).toBe(false)
16
+ })
17
+ })
18
+
19
+ describe('mergeDeep 测试', () => {
20
+ it('传入基本类型', () => {
21
+ const source = 1;
22
+ const target = 2;
23
+ const result = 2;
24
+ expect(mergeDeep(source, target)).toEqual(result)
25
+ })
26
+ it('传入数组类型', () => {
27
+ const source = [1, 2];
28
+ const target = [2, 3, 4];
29
+ const result = [2, 3, 4];
30
+ expect(mergeDeep(source, target)).toEqual(result)
31
+ })
32
+ it('传入浅层对象', () => {
33
+ const source = {name: "admin"}
34
+ const target = {name: 'system'}
35
+ const result = {name: 'system'}
36
+ expect(mergeDeep(source, target)).toEqual(result)
37
+ })
38
+ it('传入浅层对象2', () => {
39
+ const source = {name: "admin"}
40
+ const target = {name: 'admin', age: 12}
41
+ const result = {name: 'admin', age: 12}
42
+ expect(mergeDeep(source, target)).toEqual(result)
43
+ })
44
+ it('传入深层对象', () => {
45
+ const source = {name: "admin", info: {age: 1}}
46
+ const target = {name: 'system', info: {age: 12, sex: '男'}}
47
+ const result = {name: 'system', info: {age: 12, sex: '男'}}
48
+ expect(mergeDeep(source, target)).toEqual(result)
49
+ })
50
+ it('传入深层对象2', () => {
51
+ const source = {name: "admin", info: {age: [1, 2], sex: '男'}}
52
+ const target = {info: {age: [2, 3]}}
53
+ const result = {name: 'admin', info: {age: [2, 3], sex: '男'}}
54
+ expect(mergeDeep(source, target)).toEqual(result)
55
+ })
56
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "extends": "@vue/tsconfig/tsconfig.dom.json",
3
+ "compilerOptions": {
4
+ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
5
+ "types": [
6
+ "vite/client",
7
+ "vitest/globals"
8
+ ],
9
+ "declaration": true,
10
+ "declarationMap": true,
11
+ "emitDeclarationOnly": false,
12
+ "rootDir": "./src",
13
+ /* Linting */
14
+ "noUnusedLocals": true,
15
+ "noUnusedParameters": true,
16
+ "erasableSyntaxOnly": true,
17
+ "noFallthroughCasesInSwitch": true,
18
+ "baseUrl": ".",
19
+ "paths": {
20
+ "@/*": [
21
+ "src/*"
22
+ ]
23
+ }
24
+ },
25
+ "include": [
26
+ "src/**/*.ts",
27
+ "test/**/*.ts"
28
+ ]
29
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,46 @@
1
+ import {defineConfig} from "vitest/config";
2
+ import vue from '@vitejs/plugin-vue'
3
+ import {resolve} from 'path'
4
+ import dts from "vite-plugin-dts"
5
+
6
+ export default defineConfig({
7
+ plugins: [vue(),
8
+ dts({
9
+ tsconfigPath: "./tsconfig.json",
10
+ outDirs: "dist",
11
+ exclude: ["src/test"]
12
+ })
13
+ ],
14
+ build: {
15
+ lib: {
16
+ entry: resolve(__dirname, 'src/index.ts'),
17
+ formats: ['es'],
18
+ fileName: () => `index.js`
19
+ },
20
+ rollupOptions: {
21
+ external: ['vue'],
22
+ output: {
23
+ globals: {
24
+ vue: 'Vue'
25
+ },
26
+ assetFileNames: (assetInfo) => {
27
+ if (assetInfo.name?.endsWith('.css')) {
28
+ return 'index.css'; // 生成 index.css
29
+ }
30
+ return 'assets/[name]-[hash][extname]';
31
+ }
32
+ },
33
+ },
34
+ outDir: 'dist',
35
+ sourcemap: false
36
+ },
37
+ resolve: {
38
+ alias: {
39
+ '@': resolve(__dirname, 'src')
40
+ }
41
+ },
42
+ test: {
43
+ globals: true,
44
+ environment: 'jsdom',
45
+ }
46
+ })