Giter Site home page Giter Site logo

quick-format-unescaped's Introduction

quick-format-unescaped

unescaped ?

Sometimes you want to embed the results of quick-format into another string, and then escape the whole string.

usage

var format = require('quick-format-unescaped')
format('hello %s %j %d', ['world', [{obj: true}, 4, {another: 'obj'}]])

format(fmt, parameters, [options])

fmt

A printf-like format string. Example: 'hello %s %j %d'

parameters

Array of values to be inserted into the format string. Example: ['world', {obj:true}]

options.stringify

Passing an options object as the third parameter with a stringify will mean any objects will be passed to the supplied function instead of an the internal tryStringify function. This can be useful when using augmented capability serializers such as fast-safe-stringify or fast-redact.

caveats

By default quick-format-unescaped uses JSON.stringify instead of util.inspect, this means functions will not be serialized.

Benchmarks

Node 8.11.2

util*100000: 350.325ms
quick*100000: 268.141ms
utilWithTailObj*100000: 586.387ms
quickWithTailObj*100000: 280.200ms
util*100000: 325.735ms
quick*100000: 270.251ms
utilWithTailObj*100000: 492.270ms
quickWithTailObj*100000: 261.797ms

Node 10.4.0

util*100000: 301.035ms
quick*100000: 217.005ms
utilWithTailObj*100000: 404.778ms
quickWithTailObj*100000: 236.176ms
util*100000: 286.349ms
quick*100000: 214.646ms
utilWithTailObj*100000: 388.574ms
quickWithTailObj*100000: 226.036ms

Acknowledgements

Sponsored by nearForm

quick-format-unescaped's People

Contributors

artskydj avatar climba03003 avatar commenthol avatar davidmarkclements avatar hbacelar avatar jsumners avatar mcollina avatar n4zukker avatar watson avatar yaroslav-korotaev avatar zirak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quick-format-unescaped's Issues

any circular references are problematic

currently if an object has a circular reference the whole object is serialized as: [Circular]

This is a problem, first if the intended JSON is a key or some other stringify JSON it's going to cause an error on parse (since [Circular] isn't quoted) - second all other non-circular properties on the object are lost

we could use json-stringify-safe but this double or triples the benchmarks, so we need a faster version of json-stringify-safe

Error parsing when there are format string next to %d e.g %d%d or %d%s

I met this issue when I test with pino.
I noticed there is a similar issue #18
But it seems there are still some other issues when a format string next to one another.

The detail would be

const format = require('quick-format-unescaped')

console.log(format('%d%d', [11, 22]));
console.log(format('%d%s', [11, 22]));
console.log(format('%d%o', [11, { aa: 22 }]));
console.log(format('%d%d%d', [11, 22, 33]));
console.log(format('%d%d%s', [11, 22, 33]));
console.log(format('%d%o%d%s', [11, { aa: 22 }, 33, 'sss']))
console.log(format('%d%%%d', [11, 22]));
console.log(format('%d%%%s', [11, 22]));
console.log('\n');
console.log(format('%s%d', [11, 22])); // 
console.log(format('%s%s', [11, 22]));
console.log(format('%s%o', [11, { aa: 22 }]));
console.log(format('%s%d%d', [11, 22, 33]));
console.log(format('%s%d%s', [11, 22, 33]));
console.log(format('%s%o%d%s', [11, { aa: 22 }, 33, 'sss']))
console.log(format('%s%%%d', [11, 22]));
console.log(format('%s%%%s', [11, 22]));

These produce:

11%d
11%s
11%o
11%d22
11%d22
11%oNaN%s
11%%d
11%%s


1122
1122
11{"aa":22}
1122%d
1122%s
11{"aa":22}33%s
11%22
11%22

4.0.2 breaks pino

This commit: 858729a

Causes this issue: pinojs/pino#983

Reproduction:

{
  "dependencies": {
    "qfu2": "npm:[email protected]",
    "quick-format-unescaped": "4.0.1"
  }
}
'use strict'

const assert = require('assert')
const qfu = require('quick-format-unescaped')
const qfu2 = require('qfu2')

const out1 = qfu(undefined, [])
const out2 = qfu2(undefined, [])

assert.equal(out1, out2)

Missing agruments with %%

Arguments are get skipped when %% is used:

const util = require('util');
const format = require('quick-format-unescaped');

console.log(util.format('test with %%: %s', 'foo')); // -> test with %: foo
console.log(format(['test with %%: %s', 'foo'])); // -> test with %: %s

License

Could you please provide a license. I am using quick-format and have just realised there is no license mentioned.

A repository link in package.json would also be appreciated.
#4 looks like a good option

unsupported %f and %i replacements

console.log offers %f and %i as replacements which are unsupported by this package.
%f is an alternative to %d where %i floors the numeric value.

e.g.

console.log('%f', 42.9) //> 42.9
console.log('%i', 42.9) //> 42

Is it a goal of this lib to support those replacements?

Error parsing %s%s

If two %s is next to each other, the last one will be ignored:

var format = require('quick-format-unescaped')
format('%s%s', 'foo', 'bar') // => "foo%s bar"
format('%s %s', 'foo', 'bar') // => "foo bar"

v4 won't print objects unless format string is present

I recently upgraded to the latest version of pinojs, which pulls in this library. In the previous version of pino, I could do something like this:

log.info('Uhoh, an error occurred:', err);
--or--
log.info('This object was just received', { some: 'object', with: 'values' });

Which would print the string + the provided object/arguments.

I see that in quick-format-unescaped v4, this was changed to not work without using the format: log.info('This object was just received %s, { some: 'object', with: 'values' });, which ends up breaking a lot of logging code that I had.

@davidmarkclements could you elaborate what the purpose was for this change? (i.e. 7113010)

Or, is there some workaround?

Unescaped version still escape arguments

I've used pino logger and found that interpolated messages get escaped. So, it happens here.

const util = require('util');
const format = require('quick-format-unescaped');

console.log(util.format('test: %s', 'with \"qoutes\"')); // -> test: with "qoutes"
console.log(format(['test: %s', 'with \"qoutes\"'])); // -> test: with \"qoutes\"

Compatibility with util.format

I have some problem with format. This is not convenient for testing

Example:

let util = require('util');
let format = require('quick-format-unescaped');

let a = util.format('foo %o', {name: "alex"}); // foo { name: 'alex' }
let b = format('foo %o', [{name: "alex"}]); // foo {"name":"alex"}

a === b // false

Is it possible add option for change formatting?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.