-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate objects and arrays from raw source
- Loading branch information
Showing
7 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './esm' | ||
export * from './js' | ||
export * from './object' | ||
export * from './string' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { genString } from './string' | ||
|
||
export function genObjectFromRaw (obj: Record<string, any>, indent = ''): string { | ||
return genObjectFromRawEntries(Object.entries(obj), indent) | ||
} | ||
|
||
export function genArrayFromRaw (array: any[], indent = '') { | ||
const newIdent = indent + ' ' | ||
return wrapInDelimiters(array.map(i => `${newIdent}${genRawValue(i, newIdent)}`), indent, '[]') | ||
} | ||
|
||
export function genObjectFromRawEntries (array: [key: string, value: any][], indent = '') { | ||
const newIdent = indent + ' ' | ||
return wrapInDelimiters(array.map(([key, value]) => `${newIdent}${genObjectKey(key)}: ${genRawValue(value, newIdent)}`), indent, '{}') | ||
} | ||
|
||
// --- Internals --- | ||
|
||
function wrapInDelimiters (lines: string[], indent = '', delimiters = '{}') { | ||
if (!lines.length) { | ||
return delimiters | ||
} | ||
const [start, end] = delimiters | ||
return `${start}\n` + lines.join(',\n') + `\n${indent}${end}` | ||
} | ||
|
||
function genRawValue (value: unknown, indent = ''): string { | ||
if (typeof value === 'undefined') { | ||
return 'undefined' | ||
} | ||
if (value === null) { | ||
return 'null' | ||
} | ||
if (Array.isArray(value)) { | ||
return genArrayFromRaw(value, indent) | ||
} | ||
if (value && typeof value === 'object') { | ||
return genObjectFromRaw(value, indent) | ||
} | ||
return value.toString() | ||
} | ||
|
||
const VALID_IDENTIFIER_RE = /^[$_]?[\w\d]*$/ | ||
|
||
function genObjectKey (key: string) { | ||
return key.match(VALID_IDENTIFIER_RE) ? key : genString(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { CodegenOptions } from './types' | ||
|
||
export function genString (input: string, opts: CodegenOptions = {}) { | ||
const str = JSON.stringify(input) | ||
if (!opts.singleQuotes) { | ||
return JSON.stringify(input) | ||
} | ||
return `'${escapeString(str)}'` | ||
} | ||
|
||
// https://github.com/rollup/rollup/blob/master/src/utils/escapeId.ts | ||
const NEEDS_ESCAPE_RE = /[\\'\r\n\u2028\u2029]/ | ||
const QUOTE_NEWLINE_RE = /(['\r\n\u2028\u2029])/g | ||
const BACKSLASH_RE = /\\/g | ||
export function escapeString (id: string): string { | ||
if (!id.match(NEEDS_ESCAPE_RE)) { return id } | ||
return id.replace(BACKSLASH_RE, '\\\\').replace(QUOTE_NEWLINE_RE, '\\$1') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters