Skip to content

Commit

Permalink
validStringLiteral utility
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 13, 2014
1 parent 0b2a15a commit 5196ef0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/valid-string-literal.js
@@ -0,0 +1,22 @@
'use strict';

module.exports = function (t, a) {
var str;
a.throws(function () { t(); }, TypeError, "Undefined");
a.throws(function () { t(null); }, TypeError, "Null");
a.throws(function () { t({}); }, TypeError, "Object");
a.throws(function () { t(''); }, TypeError, "Empty code");
a(t('""'), '""', "Empty \" string");
a(t('\'\''), '\'\'', "Empty ' string");
a.throws(function () { t('"sdfsdf'); }, TypeError, "Not finished \" string");
a.throws(function () { t('\'sdfsdf'); }, TypeError, "Not finished ' string");
str = '\'sdf\\\'fefeefe\\\\efefe\\n\\\\\\\'\\\'efef"" "sdfdfsdf\'';
a(t(str), str, "' string");
str = '"sdf\\"fefeefe\\\\efefe\\n\\\\\\"\\"efef\'\' \'sdfdfsdf"';
a(t(str), str, "Messy \" string");
a.throws(function () { t(' "sdf\\"fefeefe\\\\efefe\\n\\\\\\"\\"efef\'\' \'sdfdfsdf"'); },
TypeError, "Starts with ws");
a.throws(function () { t('"sdf\\"fefeefe\\\\efefe\\n\\\\\\"\\"efef\'\' \'sdfdfsdf" '); },
TypeError, "Ends with ws");
a.throws(function () { t('34'); }, TypeError, "Number");
};
8 changes: 8 additions & 0 deletions valid-string-literal.js
@@ -0,0 +1,8 @@
'use strict';

var isStringLiteral = require('./is-string-literal');

module.exports = function (arg) {
if (isStringLiteral(arg)) return arg;
throw new TypeError(arg + " does not represent string literal");
};

0 comments on commit 5196ef0

Please sign in to comment.