Skip to content

Instantly share code, notes, and snippets.

View eligrey's full-sized avatar
:octocat:

Eli Grey eligrey

:octocat:
View GitHub Profile
/*
* e4x-dom.js
* Version 1.0.2
*
* e4x-dom.js makes it easy to interact with the DOM with E4X XML objects.
*
* 2009-11-18
*
* By Elijah Grey, http://eligrey.com
*
// e4x-dom.js usage examples
"use strict";
var img = <img
src={prompt("Enter an image URI")}
longdesc={prompt("Describe the image")}
id="foobar"
/>;
img.@alt = img.@longdesc;
/*
* DontEnum.js
* 2009-10-11
*
* By Elijah Grey, http://eligrey.com
*
* DontEnum.js allows you to set properties as non-enumerable.
* To set a property as non-enumerable, set someObject.DontEnum$property to true.
* Requires JavaScript 1.7 or higher.
*/
"use strict";
Array.prototype.item = function (i) {
return this[(i < 0 ? this.length + i : i) >>> 0];
};
// example: [1,2,3,4,5].item(-2) === 4
function upperFirstChar({$self, 0: firstChar})
firstChar.toUpperCase() + $self.substr(1); // no need to use arguments[0]
upperFirstChar("foobar") === "Foobar";
"use strict";
// lockTitle() returns a function that is allowed to change the title after it is locked
// This only works in browsers that support the DOMTitleChanged event (only Firefox)
var lockTitle = function () {
lockTitle = undefined;
var titleChanging = false,
title = document.title;
document.addEventListener("DOMTitleChanged", function () {
"use strict";
Function.prototype.applyCurry = function (thisp, args) {
var fn = this,
F = function () {
return fn.apply(
thisp !== null && typeof thisp !== "undefined" ? thisp : this,
args.concat(Array.prototype.slice.call(arguments))
);
};
@eligrey
eligrey / xpathresult-iterator.es
Last active May 8, 2024 21:23
Iterator implementations for XPathResult
// Requires ECMAScript 6
"use strict";
XPathResult.prototype[Symbol.iterator] = function* () {
let node;
switch (this.resultType) {
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
while (node = this.iterateNext()) {
// ECMAScript 3
"use strict";
if (typeof StopIteration !== "undefined") {
StopIteration.name = "StopIteration"; // the only exception class that doesn't have a name
StopIteration.message = "End of iteration"; // might as well give it a message too
}
Function.prototype.suppress = function () {
// This is a usage example of using function.input to asynchronously get input for
// a synchronous function. All parameters of printName can be skipped by passing undefined
// in their place. Input will be requested from the user for every parameter skipped.
var reqName = {
message: "Enter name",
defaultValue: "John Doe"
},
printName = function (name, lied) {
var [first, last] = (name || (yield [String, reqName])).split(" ");