From 8e4987eea42ef7850cac2678d4c861394b16576c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 4 Nov 2010 19:32:52 -0600 Subject: [PATCH] first (and likely only) commit --- README.md | 73 ++++++++++++++++++++++++++++ lib/remedial.js | 106 +++++++++++++++++++++++++++++++++++++++++ package.json | 12 +++++ tests/methods-exist.js | 20 ++++++++ 4 files changed, 211 insertions(+) create mode 100644 README.md create mode 100644 lib/remedial.js create mode 100644 package.json create mode 100644 tests/methods-exist.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..85fb9d3 --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +Remedial JavaScript +==== + +Douglas Crockford's `remedial.js` with a thin wrap for SSJS + +This works in both the Browser and SSJS. + + npm install remedial + + require('remedial'); + + +Usage / API +==== + +typeOf(o) + +Since JavaScript is a loosely-typed language, it is sometimes necessary to examine a value to determine its type. (This is sometimes necessary in strongly typed languages as well.) JavaScript provides a typeof operator to facilitate this, but typeof has problems. + + typeof typeOf + Object 'object' 'object' + Array 'object' 'array' + Function 'function' 'function' + String 'string' 'string' + Number 'number' 'number' + Boolean 'boolean' 'boolean' + null 'object' 'null' + undefined 'undefined' 'undefined' + +typeof [] produces 'object' instead of 'array'. That isn't totally wrong since arrays in JavaScript inherit from objects, but it isn't very useful. typeof null produces 'object' instead of 'null'. That is totally wrong. + +We can correct this by defining our own typeOf function, which we can use in place of the defective typeof operator. + +isEmpty(v) +---- + +isEmpty(v) returns true if v is an object containing no enumerable members. + +**String Methods** +---- + +JavaScript provides some useful methods for strings, but leaves out some important ones. Fortunately, JavaScript allows us to add new methods to the basic types. + +entityify() +---- + +entityify() produces a string in which '<', '>', and '&' are replaced with their HTML entity equivalents. This is essential for placing arbitrary strings into HTML texts. So, + + "if (a < b && b > c) {".entityify() + +produces + + "if (a < b && b > c) {" + +quote() +---- + +quote() produces a quoted string. This method returns a string that is like the original string except that it is wrapped in quotes and all quote and backslash characters are preceded with backslash. + +supplant(object) +---- + +supplant() does variable substitution on the string. It scans through the string looking for expressions enclosed in { } braces. If an expression is found, use it as a key on the object, and if the key has a string value or number value, it is substituted for the bracket expression and it repeats. This is useful for automatically fixing URLs. So + +param = {domain: 'valvion.com', media: 'http://media.valvion.com/'}; +url = "{media}logo.gif".supplant(param); + +produces a url containing "http://media.valvion.com/logo.gif". + +trim() +---- + +The trim() method removes whitespace characters from the beginning and end of the string. diff --git a/lib/remedial.js b/lib/remedial.js new file mode 100644 index 0000000..f4187a7 --- /dev/null +++ b/lib/remedial.js @@ -0,0 +1,106 @@ +/*jslint onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */ +"use strict"; +(function () { + function typeOf(value) { + var s = typeof value; + if (s === 'object') { + if (value) { + if (typeof value.length === 'number' && + !(value.propertyIsEnumerable('length')) && + typeof value.splice === 'function') { + s = 'array'; + } + } else { + s = 'null'; + } + } + return s; + } + + function isEmpty(o) { + var i, v; + if (typeOf(o) === 'object') { + for (i in o) { // fails jslint + v = o[i]; + if (v !== undefined && typeOf(v) !== 'function') { + return false; + } + } + } + return true; + } + + if (!String.prototype.entityify) { + String.prototype.entityify = function () { + return this.replace(/&/g, "&").replace(//g, ">"); + }; + } + + if (!String.prototype.quote) { + String.prototype.quote = function () { + var c, i, l = this.length, o = '"'; + for (i = 0; i < l; i += 1) { + c = this.charAt(i); + if (c >= ' ') { + if (c === '\\' || c === '"') { + o += '\\'; + } + o += c; + } else { + switch (c) { + case '\b': + o += '\\b'; + break; + case '\f': + o += '\\f'; + break; + case '\n': + o += '\\n'; + break; + case '\r': + o += '\\r'; + break; + case '\t': + o += '\\t'; + break; + default: + c = c.charCodeAt(); + o += '\\u00' + Math.floor(c / 16).toString(16) + + (c % 16).toString(16); + } + } + } + return o + '"'; + }; + } + + if (!String.prototype.supplant) { + String.prototype.supplant = function (o) { + return this.replace(/{([^{}]*)}/g, + function (a, b) { + var r = o[b]; + return typeof r === 'string' || typeof r === 'number' ? r : a; + } + ); + }; + } + + if (!String.prototype.trim) { + String.prototype.trim = function () { + return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1"); + }; + } + + // Boiler Plate + if ('undefined' === typeof module) { module = {}; } + module.exports = { + typeOf: typeOf, + isEmpty: isEmpty + }; + if ('undefined' === typeof global) { global = window; } + global.typeOf = global.typeOf || typeOf; + global.isEmpty = global.isEmpty || isEmpty; + if ('undefined' === typeof provide) { provide = function () {}; } + provide('remedial'); +}()); diff --git a/package.json b/package.json new file mode 100644 index 0000000..3eb3f30 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name" : "remedial", + "description" : "Douglas Crockford's Remedial JavaScript", + "url" : "http://javascript.crockford.com/remedial.html", + "keywords" : ["util", "isEmpty", "typeOf", "entityify", "quote", "supplant", "trim"], + "author" : "Douglas Crockford (packaged by AJ ONeal )", + "contributors" : [], + "dependencies" : [], + "lib" : "lib", + "main" : "./lib/remedial", + "version" : "1.0.1" +} diff --git a/tests/methods-exist.js b/tests/methods-exist.js new file mode 100644 index 0000000..6dd2549 --- /dev/null +++ b/tests/methods-exist.js @@ -0,0 +1,20 @@ +(function () { + require('../lib/remedial'); + + if ('undefined' === typeof isEmpty) { + console.log('isEmpty fail'); + } + if ('undefined' === typeof typeOf) { + console.log('typeOf fail'); + } + if ('undefined' === typeof String.prototype.entityify) { + console.log('entityify fail'); + } + if ('undefined' === typeof String.prototype.quote) { + console.log('quote fail'); + } + if ('undefined' === typeof String.prototype.supplant) { + console.log('supplant fail'); + } + +}());