diff --git a/index.js b/index.js index 2003f38..ab9e242 100644 --- a/index.js +++ b/index.js @@ -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(); }; /** @@ -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, "''"); @@ -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 + '"'; } \ No newline at end of file diff --git a/test/index.js b/test/index.js index f90c4c2..4767319 100644 --- a/test/index.js +++ b/test/index.js @@ -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'); }) }) @@ -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(); @@ -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'"); + }) })