Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copied functions from test pages into separate files
- Loading branch information
Linux User
committed
Sep 21, 2010
1 parent
8ac567e
commit c7bf83b
Showing
5 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
"use strict"; | ||
var CopyCat = CopyCat || {}; | ||
// CommonJS | ||
if ('undefined' !== typeof module && 'undefined' !== typeof exports) { | ||
module.exports = CopyCat; | ||
} | ||
|
||
(function () { | ||
//require("../vendor/remedial"); | ||
|
||
// get swap values from an args array | ||
function arguceptor(args, directive, hash) { | ||
//args = Array.prototype.slice.call(arguments), | ||
var i = 0, | ||
d, | ||
swap, | ||
a; | ||
|
||
function get_or_swap(key) { | ||
swap = a[key]; | ||
if ('null' !== typeOf(hash[key])) { | ||
a[key] = hash[d[key]]; | ||
} | ||
hash[d[key]] = swap; | ||
} | ||
for(i = 0; i < args.length; i += 1) { | ||
d = directive[i]; | ||
a = args[i]; | ||
|
||
// assume the argument exists and skip it | ||
if (true === d) { | ||
console.log('d is true'); | ||
continue; | ||
} | ||
|
||
// if there are optional params | ||
// and the length of params is less | ||
// than the length of directive | ||
// assume that it is the missing one | ||
// then decrease the length of the directive | ||
if ('undefined' === typeOf(d)) { | ||
console.log('d is undefined'); | ||
if (args.length < directive.length && 0 < directive.length) { | ||
console.log('shifting...'); | ||
directive.shift(); | ||
i -= 1; | ||
} | ||
continue; | ||
} | ||
|
||
// if we have a name for the sorry sap | ||
// then grab or swap it from the hash | ||
if ('string' === typeOf(d)) { | ||
swap = a; | ||
if ('null' !== typeOf(hash[d])) { | ||
a = hash[d]; | ||
} | ||
hash[d] = swap; | ||
continue; | ||
} | ||
|
||
// If the directive is an object | ||
// get or swap each key | ||
if ('object' === typeOf(d)) { | ||
console.log('d is object'); | ||
// but if the arg isn't an object, assume it to be optional | ||
if ('object' !== typeOf(a) && 0 < directive.length) { | ||
directive.shift(); | ||
i -= 1; | ||
continue; | ||
} | ||
Object.keys(d).forEach(get_or_swap); | ||
continue; | ||
} | ||
} | ||
return hash; | ||
} | ||
|
||
CopyCat.arguceptor = arguceptor; | ||
CopyCat.arguceptor.test = function () { | ||
/** | ||
* Immediate Goal: | ||
* $.getJSON = subscribify($.getJSON, [true, undefined,'callback', { onError : 'errback', timeout : 'timeout' }]) | ||
*/ | ||
var directive = [ | ||
true, | ||
undefined, | ||
'callback', | ||
{ | ||
onError : 'errback', | ||
timeout : 'timeout' | ||
} | ||
], | ||
hash = { | ||
'callback': function () {}, | ||
'errback': function () {}, | ||
timeout: 0 | ||
}, | ||
args = [ | ||
'http://example.com', | ||
function () { | ||
return 'original_cb'; | ||
}, | ||
{ | ||
onError: function () { | ||
return 'original_eb'; | ||
}, | ||
timeout: 42 | ||
} | ||
]; | ||
|
||
console.log(arguceptor(args, directive, hash).timeout); | ||
} | ||
}()); | ||
|
||
// [] || {} || 0 || '' || false || function(){} means that it's required | ||
// {required: 'name'} == swap values if value exists, otherwise grab | ||
// { type: [] || {} || 0 || '' || false || function(){} || null || undefined } | ||
|
||
/** | ||
* End goal: | ||
* intercepted = interceptor(map1, map2, map3)(wrapperFunc, wrappedFunc); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
"use strict"; | ||
var CopyCat = CopyCat || {}; | ||
// CommonJS Module | ||
if ('undefined' !== typeof module && 'undefined' !== typeof exports) { | ||
module.exports = CopyCat; | ||
} | ||
(function () { | ||
function format_label(s) { | ||
return s.titleize(); | ||
} | ||
function format_array_item(s) { | ||
return s.singularize(); | ||
} | ||
|
||
var labelize, | ||
singularize; | ||
|
||
// Return the string or an empty string | ||
function string_identity(s) { | ||
if ('string' !== typeOf(s)) { | ||
s = ''; | ||
} | ||
return s; | ||
} | ||
|
||
// recurse until the object is html | ||
/* | ||
* TODO - demonstrate use of merge / fold | ||
* If two people objects have different members - say email and phone | ||
* merge them into one for the sake of creating the template | ||
*/ | ||
function create_template(){ | ||
// set vars only once | ||
} | ||
|
||
function json2html(fixture, className, appendName, params) { | ||
params = params || {}; | ||
if (undefined === typeOf(className)) { | ||
className = ""; | ||
} | ||
labelize = labelize || params.labelize || string_identity; | ||
if ("function" === typeOf(params.singularize)) { | ||
singularize = params.singularize | ||
} else if (undefined === singularize) { | ||
singularize = function (s) { | ||
return string_identitiy(s) + "_instance"; | ||
} | ||
} | ||
function string2html() { | ||
var s2h = document.createElement('input'); | ||
s2h.type = 'text'; | ||
s2h.className = className; | ||
s2h.name = className; | ||
s2h.value = fixture; | ||
return s2h; | ||
} | ||
function array2html() { | ||
var i, | ||
ii, | ||
ul = document.createElement('ul'), | ||
li; | ||
className = singularize(className); | ||
ul.className = className; | ||
for (i = 0, ii = fixture.length; i < ii; i += 1) { | ||
li = document.createElement('li'); | ||
li.appendChild(json2html(fixture[i], className, false)); | ||
ul.appendChild(li); | ||
} | ||
return ul; | ||
} | ||
function object2html() { | ||
var pdiv = document.createElement('div'), | ||
div, | ||
key; | ||
pdiv.className = className; | ||
for (key in fixture) { | ||
if (fixture.hasOwnProperty(key)) { | ||
div = document.createElement('div'); | ||
var p = json2html(fixture[key], key, true); | ||
var s = document.createElement('span'); | ||
s.className='auto_title'; | ||
s.appendChild(document.createTextNode(labelize(key) + ": ")); | ||
div.appendChild(s); | ||
div.appendChild(p); | ||
div.className = key + '_container'; | ||
pdiv.appendChild(div); | ||
} | ||
} | ||
return pdiv; | ||
} | ||
if ("string" === typeOf(fixture)) { | ||
return string2html(); | ||
} else if ("array" === typeOf(fixture)) { | ||
return array2html(); | ||
} else if ("object" === typeOf(fixture)) { | ||
return object2html(); | ||
} else if ("function" === typeOf(fixture)) { | ||
alert("functions are not supported"); | ||
return; | ||
} else { | ||
alert("some weird crap happened"); | ||
} | ||
} | ||
|
||
CopyCat.autotemplate = json2html; | ||
CopyCat.autotemplate.test = function () { | ||
document.body.insertBefore(json2html(fixture, "contacts", undefined, { | ||
labelize: format_label, | ||
singularize: format_array_item | ||
}), document.body.firstChild); | ||
} | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"use strict"; | ||
var CopyCat = CopyCat || {}; | ||
// CommonJS | ||
if ('undefined' !== typeof module && 'undefined' !== typeof exports) { | ||
module.exports = CopyCat; | ||
} | ||
|
||
(function () { | ||
var fsomething, | ||
fliteral, | ||
farray, | ||
fobject; | ||
|
||
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; | ||
} // Thanks to Crockford | ||
|
||
fsomething = function (key, val) { | ||
switch(typeOf(val)) { | ||
case "object": | ||
return fobject(key, val); | ||
break; | ||
case "array": | ||
return farray(key, val); | ||
break; | ||
default: | ||
return fliteral(key, val); | ||
} | ||
} | ||
|
||
fliteral = function (key, val) { | ||
var o = {}; | ||
o[key] = val.toString(); | ||
return o; | ||
}; | ||
|
||
farray = function (key, arr) { | ||
var o = {}; | ||
o[key] = "[]"; | ||
arr.forEach(function (val, i) { | ||
var nobj = fsomething("[" + i + "]", val); | ||
Object.keys(nobj).forEach(function (nkey) { | ||
o[key + nkey] = nobj[nkey]; | ||
}); | ||
}); | ||
return o; | ||
} | ||
|
||
fobject = function (skey, obj) { | ||
var o = {}; | ||
o[skey] = "{}"; | ||
Object.keys(obj).forEach(function (key) { | ||
var nobj = fsomething('.' + key, obj[key]); | ||
Object.keys(nobj).forEach(function (nkey) { | ||
o[skey + nkey] = nobj[nkey]; | ||
}); | ||
}); | ||
return o; | ||
} | ||
|
||
CopyCat.flatten = fsomething; | ||
CopyCat.flatten.test = function () { | ||
console.log(JSON.stringify(fsomething("obj", {str:"stringy",num:1,arr:[1,2,[4,5,6]]}), null, ' ')); | ||
} | ||
}()); |
Oops, something went wrong.