Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions 34 src/helpers/code-builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const util = require('util')
const formatString = require('./format')

/**
* Helper object to format and aggragate lines of code.
Expand All @@ -13,6 +13,7 @@ const util = require('util')
*/
const CodeBuilder = function (indentation, join) {
this.code = []
this.indentLevel = 0
this.indentation = indentation
this.lineJoin = join || '\n'
}
Expand Down Expand Up @@ -42,7 +43,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
if (Object.prototype.toString.call(indentationLevel) === '[object String]') {
slice = 1
line = indentationLevel
indentationLevel = 0
indentationLevel = this.indentLevel
} else if (indentationLevel === null) {
return null
}
Expand All @@ -55,7 +56,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
const format = Array.prototype.slice.call(arguments, slice, arguments.length)
format.unshift(lineIndentation + line)

return util.format.apply(this, format)
return formatString.apply(this, format)
}

/**
Expand Down Expand Up @@ -100,4 +101,31 @@ CodeBuilder.prototype.join = function () {
return this.code.join(this.lineJoin)
}

/**
* Increase indentation level
* @returns {this}
*/
CodeBuilder.prototype.indent = function () {
this.indentLevel++
return this
}

/**
* Decrease indentation level
* @returns {this}
*/
CodeBuilder.prototype.unindent = function () {
this.indentLevel--
return this
}

/**
* Reset indentation level
* @returns {this}
*/
CodeBuilder.prototype.reindent = function () {
this.indentLevel = 0
return this
}

module.exports = CodeBuilder
53 changes: 53 additions & 0 deletions 53 src/helpers/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const util = require('util')

function quote (value) {
if (typeof value !== 'string') value = JSON.stringify(value)
return JSON.stringify(value)
}

function escape (value) {
const q = quote(value)
return q && q.slice(1, -1)
}

/**
* Wraps the `util.format` function and adds the %q and %v format options,
* where `%q` - escape tricky characters, like newline or quotes
* and `%v` - JSON-stringify-if-necessary
*
* @param {string} value
* @param {...string} format
*
* @example
* format('foo("%q")', { bar: 'baz' })
* // output: foo("{\"bar\":\"baz\"}")
*
* format('foo(%v)', { bar: 'baz' })
* // output: foo({"bar":"baz"})
*
* @returns {string} Formatted string
*/
function format (value, ...format) {
if (typeof value !== 'string') return ''

let i = 0
value = value.replace(/(?<!%)%[sdifjoOcqv]/g, (m) => {
// JSON-stringify
if (m === '%v') {
const [elem] = format.splice(i, 1)
return JSON.stringify(elem)
}
// JSON-stringify, remove quotes (means, escape)
if (m === '%q') {
const [elem] = format.splice(i, 1)
return escape(elem)
}
i += 1
return m
})

const ret = util.format(value, ...format)
return ret
}

module.exports = format
Morty Proxy This is a proxified and sanitized view of the page, visit original site.