Skip to content

Commit

Permalink
Move util functions in a separate file (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Nov 4, 2020
1 parent 5c444ee commit 83ef058
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 98 deletions.
111 changes: 13 additions & 98 deletions src/json-repair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
'use strict'
import {
insertAtIndex,
insertBeforeLastWhitespace,
isAlpha,
isDigit,
isHex,
isQuote,
isSpecialWhitespace,
isWhitespace,
normalizeQuote,
normalizeWhitespace,
stripLastOccurrence
} from './stringUtils.js'

// token types enumeration
const DELIMITER = 0
Expand Down Expand Up @@ -50,20 +62,6 @@ const CONTROL_CHARACTERS = {
'\t': '\\t'
}

const SINGLE_QUOTES = [
'\'', // quote
'\u2018', // quote left
'\u2019', // quote right
'\u0060', // grave accent
'\u00B4' // acute accent
]

const DOUBLE_QUOTES = [
'"',
'\u201C', // double quote left
'\u201D' // double quote right
]

const SYMBOLS = {
null: 'null',
true: 'true',
Expand Down Expand Up @@ -676,86 +674,3 @@ function parseEnd () {
throw createSyntaxError('Value expected')
}
}

/**
* Check if the given character contains an alpha character, a-z, A-Z, _
*/
function isAlpha (c: string) : boolean {
return /^[a-zA-Z_]$/.test(c)
}

/**
* Check if the given character contains a hexadecimal character 0-9, a-f, A-F
*/
function isHex (c: string) : boolean {
return /^[0-9a-fA-F]$/.test(c)
}

/**
* checks if the given char c is a digit
*/
function isDigit (c: string) : boolean {
return (c >= '0' && c <= '9')
}

/**
* Check if the given character is a whitespace character like space, tab, or
* newline
*/
function isWhitespace (c: string) : boolean {
return c === ' ' || c === '\t' || c === '\n' || c === '\r'
}

function isSpecialWhitespace (c: string) : boolean {
return (
c === '\u00A0' ||
(c >= '\u2000' && c <= '\u200A') ||
c === '\u202F' ||
c === '\u205F' ||
c === '\u3000'
)
}

function normalizeWhitespace (text: string) : string {
let normalized = ''

for (let i = 0; i < text.length; i++) {
const char = text[i]
normalized += isSpecialWhitespace(char)
? ' '
: char
}

return normalized
}

function isQuote (c: string) : boolean {
return SINGLE_QUOTES.includes(c) || DOUBLE_QUOTES.includes(c)
}

function normalizeQuote (c: string) : string {
if (SINGLE_QUOTES.includes(c)) {
return '\''
}

if (DOUBLE_QUOTES.includes(c)) {
return '"'
}

return c
}

function stripLastOccurrence (text: string, textToStrip: string) : string {
const index = output.lastIndexOf(textToStrip)
return (index !== -1)
? text.substring(0, index) + text.substring(index + 1)
: text
}

function insertBeforeLastWhitespace (text: string, textToInsert: string) {
return text.replace(/\s*$/, match => textToInsert + match)
}

function insertAtIndex (text: string, textToInsert: string, index: number): string {
return text.substring(0, index) + textToInsert + text.substring(index)
}
97 changes: 97 additions & 0 deletions src/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

const SINGLE_QUOTES = [
'\'', // quote
'\u2018', // quote left
'\u2019', // quote right
'\u0060', // grave accent
'\u00B4' // acute accent
]

const DOUBLE_QUOTES = [
'"',
'\u201C', // double quote left
'\u201D' // double quote right
]

/**
* Check if the given character contains an alpha character, a-z, A-Z, _
*/
export function isAlpha (c: string) : boolean {
return /^[a-zA-Z_]$/.test(c)
}

/**
* Check if the given character contains a hexadecimal character 0-9, a-f, A-F
*/
export function isHex (c: string) : boolean {
return /^[0-9a-fA-F]$/.test(c)
}

/**
* checks if the given char c is a digit
*/
export function isDigit (c: string) : boolean {
return (c >= '0' && c <= '9')
}

/**
* Check if the given character is a whitespace character like space, tab, or
* newline
*/
export function isWhitespace (c: string) : boolean {
return c === ' ' || c === '\t' || c === '\n' || c === '\r'
}

export function isSpecialWhitespace (c: string) : boolean {
return (
c === '\u00A0' ||
(c >= '\u2000' && c <= '\u200A') ||
c === '\u202F' ||
c === '\u205F' ||
c === '\u3000'
)
}

export function normalizeWhitespace (text: string) : string {
let normalized = ''

for (let i = 0; i < text.length; i++) {
const char = text[i]
normalized += isSpecialWhitespace(char)
? ' '
: char
}

return normalized
}

export function isQuote (c: string) : boolean {
return SINGLE_QUOTES.includes(c) || DOUBLE_QUOTES.includes(c)
}

export function normalizeQuote (c: string) : string {
if (SINGLE_QUOTES.includes(c)) {
return '\''
}

if (DOUBLE_QUOTES.includes(c)) {
return '"'
}

return c
}

export function stripLastOccurrence (text: string, textToStrip: string) : string {
const index = text.lastIndexOf(textToStrip)
return (index !== -1)
? text.substring(0, index) + text.substring(index + 1)
: text
}

export function insertBeforeLastWhitespace (text: string, textToInsert: string) {
return text.replace(/\s*$/, match => textToInsert + match)
}

export function insertAtIndex (text: string, textToInsert: string, index: number): string {
return text.substring(0, index) + textToInsert + text.substring(index)
}

0 comments on commit 83ef058

Please sign in to comment.