@ditojs/utils 2.49.0 → 2.51.0

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.
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@ditojs/utils",
3
- "version": "2.49.0",
3
+ "version": "2.51.0",
4
4
  "type": "module",
5
5
  "description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
6
6
  "repository": "https://github.com/ditojs/dito/tree/master/packages/utils",
7
7
  "author": "Jürg Lehni <juerg@scratchdisk.com> (http://scratchdisk.com)",
8
8
  "license": "MIT",
9
9
  "main": "./src/index.js",
10
+ "types": "./types/index.d.ts",
10
11
  "exports": {
11
- ".": "./src/index.js"
12
+ ".": {
13
+ "types": "./types/index.d.ts",
14
+ "import": "./src/index.js",
15
+ "require": "./src/index.js"
16
+ }
12
17
  },
13
18
  "files": [
14
19
  "src/",
@@ -34,6 +39,5 @@
34
39
  "devDependencies": {
35
40
  "typescript": "^5.8.3"
36
41
  },
37
- "types": "types",
38
- "gitHead": "8276ff1db64a82c03e1569e2403f378b0201ea3c"
42
+ "gitHead": "0b8f114647510db71c50be9fdc331297b1e726ab"
39
43
  }
@@ -9,6 +9,7 @@ exports[`index > exports all symbols 1`] = `
9
9
  "camelize": [Function],
10
10
  "capitalize": [Function],
11
11
  "clone": [Function],
12
+ "convertToJson": [Function],
12
13
  "debounce": [Function],
13
14
  "debounceAsync": [Function],
14
15
  "decamelize": [Function],
package/src/index.js CHANGED
@@ -8,3 +8,4 @@ export * from './promise/index.js'
8
8
  export * from './function/index.js'
9
9
  export * from './dataPath/index.js'
10
10
  export * from './html/index.js'
11
+ export * from './json/index.js'
@@ -0,0 +1,27 @@
1
+ import { isArray, isPlainObject } from '../base/index.js'
2
+
3
+ export function convertToJson(arg) {
4
+ return arg?.toJSON
5
+ ? arg.toJSON()
6
+ : isPlainObject(arg)
7
+ ? convertToJsonObject(arg)
8
+ : isArray(arg)
9
+ ? convertToJsonArray(arg)
10
+ : arg
11
+ }
12
+
13
+ function convertToJsonObject(arg) {
14
+ const json = {}
15
+ for (const key of Object.keys(arg)) {
16
+ json[key] = convertToJson(arg[key])
17
+ }
18
+ return json
19
+ }
20
+
21
+ function convertToJsonArray(arg) {
22
+ const ret = new Array(arg.length)
23
+ for (let i = 0, l = arg.length; i < l; ++i) {
24
+ ret[i] = convertToJson(arg[i])
25
+ }
26
+ return ret
27
+ }
@@ -0,0 +1 @@
1
+ export * from './convertToJson.js'