[ANN] npm install remedial - Crockford's remedial.js

69 views
Skip to first unread message

AJ ONeal

unread,
Nov 4, 2010, 6:48:15 PM11/4/10
to node.js mailing list
I just stuck Douglas Crockfords 'remedial.js'[1] on npm

npm install remedial

isEmpty - reliably tells you if an object or array is empyt
typeOf - reliably tells you the type of something
String.prototype.entityify -escapes "<" and ">" for html
String.prototype.quote -escapes quotes
String.prototype.supplant - templates out object names in {}

[1] http://javascript.crockford.com/remedial.html

AJ ONeal

Tane Piper

unread,
Nov 4, 2010, 7:21:15 PM11/4/10
to nod...@googlegroups.com
+1, good module to have in userland

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>

Jorge

unread,
Nov 5, 2010, 6:08:57 AM11/5/10
to nod...@googlegroups.com
a= Object.create( [] ); a[2]= 27; a
-> [ undefined, undefined, 27 ]
a.length
-> 0       // wtf ?
typeOf(a)  // the typeOf() function in remedial.js
-> 'array' // Sure ?

Any thoughts ?
-- 
Jorge.

AJ ONeal

unread,
Nov 5, 2010, 12:53:27 PM11/5/10
to nod...@googlegroups.com
a= Object.create( [] ); a[2]= 27; a
-> [ undefined, undefined, 27 ]
a.length
-> 0       // wtf ?
typeOf(a)  // the typeOf() function in remedial.js
-> 'array' // Sure ?

Any thoughts ?

Yes. My thought is that it's a perfect candidate for the WTFJS blog.

I did a little more testing in both Node and Firefox:
https://github.com/coolaj86/ssjs-remedial/blob/master/tests/array-wtf.js

If you use the `push()` method instead of `[x] = a` it then is seen as an object.

Would you expect it to be an array or an object?

Jorge

unread,
Nov 5, 2010, 6:02:35 PM11/5/10
to nod...@googlegroups.com
Fake arrays (Array-like objects) are not true arrays... so the test for array-ness in typeOf() is wrong :

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;
}


it should better be (IMO) the Miller Device http://bit.ly/cqIf0h :

function typeOf(value) {
  var s = typeof value;
  if (s === 'object') {
    if (value) {
      if ((/array/i).test(({}).toString.call(value))) {
        s = 'array';
      }
    } else {
      s = 'null';
    }
  }
  return s;
}

But that page (remedial.js) predates the discovery of the Miller device.
-- 
Jorge.

Arnout Kazemier

unread,
Nov 5, 2010, 6:03:57 PM11/5/10
to nod...@googlegroups.com
Node already exposes Array.isArray() so you might as well use that function instead. 
--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Arnout Kazemier



Jorge

unread,
Nov 5, 2010, 6:08:31 PM11/5/10
to nodejs
On Nov 5, 11:03 pm, Arnout Kazemier <i...@3rd-eden.com> wrote:
> Node already exposes Array.isArray() so you might as well use that function instead.

I know, but "In the source code that follows, the priorities were
**portability** and compactness."
( http://javascript.crockford.com/remedial.html )

:-)

Cheers,
--
Jorge.

Sami Samhuri

unread,
Nov 6, 2010, 1:49:03 AM11/6/10
to nodejs
Here's a slight variation of typeOf from cloudhead's eyes library:

function typeOf(value) {
var s = typeof(value),
types = ['Object', 'Array', 'String', 'RegExp', 'Number',
'Function', 'Boolean', 'Date'],
i;

if (s === 'object' || s === 'function') {
if (value) {
for (i = 0; i < types.length; ++i) {
if (({}).toString.call(value) === '[object ' +
types[i] + ']') {
s = types[i].toLowerCase();
}
}
} else { s = 'null' }
}
return s;
}

I think that's portable. Fairly compact. And unlike typeOf from
remedial it works on things like new String('foo').

-s

Oleg Slobodskoi

unread,
Nov 6, 2010, 6:01:07 AM11/6/10
to nod...@googlegroups.com
You should take a look at jquerys latest "type" implementation


Line 498

regards,
Oleg
@oleg008


2010/11/6 Sami Samhuri <sami.s...@gmail.com>

Mark S. Miller

unread,
Nov 6, 2010, 2:01:03 PM11/6/10
to nod...@googlegroups.com
Turns out that the discovery of the so-called "Miller device" predates my independent discovery of it, and thus of Crock's crediting me with it. It was first discovered by David Flanagan and appears in his "JavaScript: The Definitive Guide". It should be called the "Flanagan device". Please help hunt down and correct all these mis-attributions. Thanks.
 

--
    Cheers,
    --MarkM

Sami Samhuri

unread,
Nov 6, 2010, 8:18:49 PM11/6/10
to nodejs
On Nov 6, 3:01 am, Oleg Slobodskoi <oleg...@googlemail.com> wrote:
> You should take a look at jquerys latest "type" implementation
>
> https://github.com/jquery/jquery/blob/master/src/core.js
>
> Line 498

More efficient, and readable. Thanks.

-s

AJ ONeal

unread,
Nov 30, 2010, 4:00:52 AM11/30/10
to nod...@googlegroups.com
I finally fixed this issue and republished on npm.

I stole the snippet from jQuery.type.

AJ ONeal


--
Reply all
Reply to author
Forward
0 new messages