node-tosource icon indicating copy to clipboard operation
node-tosource copied to clipboard

Change double quotes to single quotes

Open piranna opened this issue 11 years ago • 6 comments

Standard on Javascript for source code are the single quotes (') instead of the double quote (") to distingish from HTML attributes, and in fact Node.js use single quotes when showing an object on the console. Double quoted strings has the advantage that they can be easily generated with JSON.stringify(), but it should be better to use single quotes and the encode() function or another similar one.

piranna avatar Jun 28 '14 00:06 piranna

+1

rus0000 avatar Apr 27 '15 13:04 rus0000

PRs welcome! :-)

It'd be even better to switch between single/double based on the string contents to minimize output length, but there are perf considerations to all of this.

marcello3d avatar Apr 27 '15 15:04 marcello3d

+1 :smile:

jamesmorgan avatar Jan 25 '16 16:01 jamesmorgan

Maybe to-single-quotes is a better choice than modifying this module.

const toSingleQuotes = require('to-single-quotes');
 
toSingleQuotes('I love "unicorns" \'and\' "ponies"');
//=> "I love 'unicorns' 'and' 'ponies'" 

banyudu avatar Oct 25 '17 14:10 banyudu

I love 'unicorns' 'and' 'ponies'

I don't like the change that does inside the string of the double quotes...

piranna avatar Oct 25 '17 14:10 piranna

It seems to-single-quotes won't modify double quotes inside quotes. So it won't produce a change of the string value.

Sample code:

const tosource = require('tosource')
const singlequote = require('to-single-quotes')

const obj = {
  key: 'str value with "double quotes"',
}

const source = tosource(obj);
console.log(source)
console.log('---------------')
console.log(singlequote(source))

Output is:

{ key:"str value with \"double quotes\"" }
---------------
{ key:'str value with "double quotes"' }

banyudu avatar Oct 25 '17 14:10 banyudu