Skip to content

Commit

Permalink
feat: add toDebugString
Browse files Browse the repository at this point in the history
  • Loading branch information
will-stone committed Dec 27, 2023
1 parent 1ab112a commit 034e213
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rich-peas-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tings': minor
---

Added `toDebugString`.
14 changes: 14 additions & 0 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@will-stone/eslint-config": "^0.12.2",
"@will-stone/eslint-config": "^0.13.0",
"@will-stone/prettier-config": "^7.0.2",
"concurrently": "^8.2.2",
"eslint": "^8.56.0",
Expand Down
3 changes: 3 additions & 0 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions package/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
58 changes: 58 additions & 0 deletions package/source/to-debug-string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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'],
[/^regex$/u, '/^regex$/u'],
// eslint-disable-next-line prefer-regex-literals
[new RegExp('ab+c', 'u'), '/ab+c/u'],
[() => 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)
})
37 changes: 37 additions & 0 deletions package/source/to-debug-string.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 034e213

Please sign in to comment.