Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.
Open
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
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ function format(fmt) {
*/

exports.string = function(val){
return null == val ? '' : String(val);
if (null == val) return '';
if (false === val) return 'f';
if (true === val) return 't';
if (val instanceof Date) return val.toISOString();
return val.toString();
};

/**
Expand All @@ -81,6 +85,13 @@ exports.ident = function(val){

exports.literal = function(val){
if (null == val) return 'NULL';
if (false === val) return "'f'";
if (true === val) return "'t'";
if (val instanceof Date) {
val = val.toISOString();
} else {
val = val.toString();
}
var backslash = ~val.indexOf('\\');
var prefix = backslash ? 'E' : '';
val = val.replace(/'/g, "''");
Expand Down Expand Up @@ -110,6 +121,13 @@ function validIdent(id) {
*/

function quoteIdent(id) {
if (false === id) return '"f"';
if (true === id) return '"t"';
if (id instanceof Date) {
id = id.toISOString();
} else {
id = id.toString();
}
id = id.replace(/"/g, '""');
return '"' + id + '"';
}
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ describe('escape(fmt, ...)', function(){

describe('escape.string(val)', function(){
it('should coerce to a string', function(){
var date = new Date(Date.UTC(2012, 11, 14, 13, 6, 43, 152));
escape.string().should.equal('');
escape.string(0).should.equal('0');
escape.string(15).should.equal('15');
escape.string(45.13).should.equal('45.13');
escape.string(true).should.equal('t');
escape.string(false).should.equal('f');
escape.string(date).should.equal('2012-12-14T13:06:43.152Z');
escape.string('something').should.equal('something');
})
})
Expand All @@ -64,6 +69,21 @@ describe('escape.ident(val)', function(){
escape.ident('cross').should.equal('"cross"');
})

it('should quote booleans', function(){
escape.ident(true).should.equal('"t"');
escape.ident(false).should.equal('"f"');
})

it('should quote numbers', function(){
escape.ident(45).should.equal('"45"');
escape.ident(45.13).should.equal('"45.13"');
})

it('should convert Date to ISO and quote it', function(){
var date = new Date(Date.UTC(2012, 11, 14, 13, 6, 43, 152));
escape.ident(date).should.equal('"2012-12-14T13:06:43.152Z"');
})

it('should throw when null', function(done){
try {
escape.ident();
Expand Down Expand Up @@ -91,5 +111,20 @@ describe('escape.literal(val)', function(){
it('should escape backslashes', function(){
escape.literal('\\whoop\\').should.equal("E'\\\\whoop\\\\'");
})

it('should quote booleans', function(){
escape.literal(true).should.equal("'t'");
escape.literal(false).should.equal("'f'");
})

it('should quote numbers', function(){
escape.literal(45).should.equal("'45'");
escape.literal(45.13).should.equal("'45.13'");
})

it('should convert Date to ISO and quote it', function(){
var date = new Date(Date.UTC(2012, 11, 14, 13, 6, 43, 152));
escape.literal(date).should.equal("'2012-12-14T13:06:43.152Z'");
})
})