diff --git a/.changeset/rich-peas-return.md b/.changeset/rich-peas-return.md new file mode 100644 index 0000000..beb9cf0 --- /dev/null +++ b/.changeset/rich-peas-return.md @@ -0,0 +1,5 @@ +--- +'tings': minor +--- + +Added `toDebugString`. diff --git a/package-lock.json b/package-lock.json index 78d6777..6375a19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13787,6 +13787,9 @@ "name": "tings", "version": "9.1.0", "license": "MIT", + "dependencies": { + "is-plain-obj": "^4.1.0" + }, "devDependencies": { "@types/node": "^20.10.5", "rimraf": "^5.0.5", @@ -13805,6 +13808,17 @@ "undici-types": "~5.26.4" } }, + "package/node_modules/is-plain-obj": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz", + "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "package/node_modules/vite": { "version": "5.0.10", "resolved": "https://registry.npmjs.org/vite/-/vite-5.0.10.tgz", diff --git a/package/package.json b/package/package.json index 7652523..102d311 100644 --- a/package/package.json +++ b/package/package.json @@ -30,6 +30,9 @@ "test": "vitest", "typecheck": "tsc --noEmit" }, + "dependencies": { + "is-plain-obj": "^4.1.0" + }, "devDependencies": { "@types/node": "^20.10.5", "rimraf": "^5.0.5", diff --git a/package/source/index.ts b/package/source/index.ts index 2ad30f0..95609b1 100644 --- a/package/source/index.ts +++ b/package/source/index.ts @@ -10,6 +10,7 @@ export { checkUrlAbsolute } from './check-url-absolute.js' export { generateIntegers } from './generate-integers.js' export { sleep } from './sleep.js' export { toCompactCase } from './to-compact-case.js' +export { toDebugString } from './to-debug-string.js' export { toLetters } from './to-letters.js' export { toNumber } from './to-number.js' export { toOrdinal } from './to-ordinal.js' diff --git a/package/source/to-debug-string.test.ts b/package/source/to-debug-string.test.ts new file mode 100644 index 0000000..8401275 --- /dev/null +++ b/package/source/to-debug-string.test.ts @@ -0,0 +1,55 @@ +import { expect, test } from 'vitest' + +import { toDebugString } from './to-debug-string.js' + +const cases = [ + ['4', '4'], + [undefined, 'undefined'], + [null, 'null'], + [Symbol(42), 'Symbol(42)'], + [false, 'false'], + [true, 'true'], + [0, '0'], + [BigInt('123'), '123n'], + [() => null, '() => null'], + [(a: unknown) => a, '(a) => a'], + [ + function func() { + return 'string' + }, + `function func() { + return "string"; + }`, + ], + [ + { + a: '4', + b: undefined, + c: null, + d: Symbol(42), + e: false, + f: true, + g: 0, + h: BigInt('100000000000000000000000000123'), + i: { a: undefined }, + }, + '{"a":"4","b":"undefined","c":"null","d":"Symbol(42)","e":"false","f":"true","g":"0","h":"100000000000000000000000000123n","i":{"a":"undefined"}}', + ], + [ + [ + '4', + undefined, + null, + Symbol(42), + false, + true, + 0, + { a: undefined, m: BigInt('100000000000000000000000000123') }, + ], + '["4","undefined","null","Symbol(42)","false","true","0",{"a":"undefined","m":"100000000000000000000000000123n"}]', + ], +] as const + +test.each(cases)('%s is %s', (input, expected) => { + expect(toDebugString(input)).toBe(expected) +}) diff --git a/package/source/to-debug-string.ts b/package/source/to-debug-string.ts new file mode 100644 index 0000000..b0c98be --- /dev/null +++ b/package/source/to-debug-string.ts @@ -0,0 +1,37 @@ +import isPlainObject from 'is-plain-obj' + +/** + * WIP + * @category String + * @example +```ts +import { toDebugString } from 'tings' + +WIP +``` + */ +export function toDebugString(input: unknown): string { + if (typeof input === 'string') { + return input + } + + if (typeof input === 'bigint') { + return `${input.toString()}n` + } + + if (isPlainObject(input)) { + return `{${Object.entries(input).map(([key, value]) => + isPlainObject(value) + ? `"${key}":${toDebugString(value)}` + : `"${key}":"${toDebugString(value)}"`, + )}}` + } + + if (Array.isArray(input)) { + return `[${input.map((value) => + isPlainObject(value) ? toDebugString(value) : `"${toDebugString(value)}"`, + )}]` + } + + return String(input) +}