Comparing version 0.2.3 to 0.2.4
261
jymin.js
/** | ||
* _ _ ___ ____ _____ | ||
* | |_ _ _ __ ___ (_)_ __ __ __/ _ \ |___ \ |___ / | ||
* _ | | | | | '_ ` _ \| | '_ \ \ \ / / | | | __) | |_ \ | ||
* | |_| | |_| | | | | | | | | | | \ V /| |_| | / __/ _ ___) | | ||
* \___/ \__, |_| |_| |_|_|_| |_| \_/ \___(_)_____(_)____/ | ||
* _ _ ___ ____ _ _ | ||
* | |_ _ _ __ ___ (_)_ __ __ __/ _ \ |___ \ | || | | ||
* _ | | | | | '_ ` _ \| | '_ \ \ \ / / | | | __) || || |_ | ||
* | |_| | |_| | | | | | | | | | | \ V /| |_| | / __/ |__ _| | ||
* \___/ \__, |_| |_| |_|_|_| |_| \_/ \___(_)_____(_) |_| | ||
* |___/ | ||
@@ -32,3 +32,3 @@ * | ||
this.jymin = {version: '0.2.3'}; | ||
this.jymin = {version: '0.2.4'}; | ||
@@ -39,6 +39,15 @@ /** | ||
var doNothing = function () {}; | ||
var globalResponseSuccessHandler = doNothing; | ||
var globalResponseFailureHandler = doNothing; | ||
var responseSuccessHandler = doNothing; | ||
var responseFailureHandler = doNothing; | ||
/** | ||
* Get an XMLHttpRequest object. | ||
*/ | ||
var getXhr = function () { | ||
var Xhr = window.XMLHttpRequest; | ||
var ActiveX = window.ActiveXObject; | ||
return Xhr ? new Xhr() : (ActiveX ? new ActiveX('Microsoft.XMLHTTP') : false); | ||
}; | ||
/** | ||
* Make an AJAX request, and handle it with success or failure. | ||
@@ -51,8 +60,6 @@ * @return boolean: True if AJAX is supported. | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function|: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean|: Whether to evaluate the response as JSON. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
// If the optional body argument is omitted, shuffle it out. | ||
if (isFunction(body)) { | ||
evalJson = onFailure; | ||
onFailure = onSuccess; | ||
@@ -62,12 +69,3 @@ onSuccess = body; | ||
} | ||
var request; | ||
if (window.XMLHttpRequest) { | ||
request = new XMLHttpRequest(); | ||
} | ||
else if (window.ActiveXObject) { | ||
request = new ActiveXObject('Microsoft.XMLHTTP'); // jshint ignore:line | ||
} | ||
else { | ||
return false; | ||
} | ||
var request = getXhr(); | ||
if (request) { | ||
@@ -80,29 +78,9 @@ request.onreadystatechange = function() { | ||
var callback = isSuccess ? | ||
onSuccess || globalResponseSuccessHandler : | ||
onFailure || globalResponseFailureHandler; | ||
var response = request.responseText; | ||
if (evalJson) { | ||
var object; | ||
if (status) { | ||
try { | ||
// Trick UglifyJS into thinking there's no eval. | ||
var evil = window.eval; // jshint ignore:line | ||
evil('eval.J=' + response); | ||
object = evil.J; | ||
} | ||
catch (e) { | ||
//+env:debug,dev | ||
error('Jymin: Could not parse JSON: "' + response + '"'); | ||
//-env:debug,dev | ||
object = {_ERROR: '_BAD_JSON', _TEXT: response}; | ||
} | ||
} | ||
else { | ||
object = {_ERROR: '_OFFLINE'}; | ||
} | ||
object._STATUS = status; | ||
object.request = request; | ||
response = object; | ||
} | ||
callback(response, request); | ||
onSuccess || responseSuccessHandler : | ||
onFailure || responseFailureHandler; | ||
var data = parse(request.responseText); | ||
data._STATUS = status; | ||
data._REQUEST = request; | ||
data = data || {_ERROR: '_OFFLINE'}; | ||
callback(data); | ||
} | ||
@@ -118,13 +96,12 @@ }; | ||
// Record the original request URL. | ||
request.url = url; | ||
request._URL = url; | ||
// TODO: Populate request.query with URL query params. | ||
// If it's a post, record the post body. | ||
if (body) { | ||
request.body = body; | ||
request._BODY = body; | ||
} | ||
// | ||
request._TIME = new Date(); | ||
// Record the time the request was made. | ||
request._TIME = getTime(); | ||
request.send(body || null); | ||
@@ -134,21 +111,8 @@ } | ||
}; | ||
/** | ||
* Request a JSON resource with a given URL. | ||
* @return boolean: True if AJAX is supported. | ||
*/ | ||
var getJson = function ( | ||
url, // string: The URL to request a response from. | ||
body, // object|: Data to post. The method is automagically "POST" if body is truey, otherwise "GET". | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
return getResponse(url, body, onSuccess, onFailure, true); | ||
}; | ||
/** | ||
* Iterate over an array, and call a function on each item. | ||
*/ | ||
var forEach = function ( | ||
array, // Array*: The array to iterate over. | ||
callback // Function*: The function to call on each item. `callback(item, index, array)` | ||
array, // Array: The array to iterate over. | ||
callback // Function: The function to call on each item. `callback(item, index, array)` | ||
) { | ||
@@ -174,3 +138,3 @@ if (array) { | ||
for (var key in object) { | ||
var result = callback(object[key], key, object); | ||
var result = callback(key, object[key], object); | ||
if (result === false) { | ||
@@ -191,3 +155,3 @@ break; | ||
if (object && decorations) { | ||
forIn(decorations, function (value, key) { | ||
forIn(decorations, function (key, value) { | ||
object[key] = value; | ||
@@ -268,2 +232,14 @@ }); | ||
/** | ||
* Pop an item off an array. | ||
* @return mixed: Popped item. | ||
*/ | ||
var pop = function ( | ||
array // Array: The array to push the item into. | ||
) { | ||
if (isArray(array)) { | ||
return array.pop(); | ||
} | ||
}; | ||
var merge = function ( | ||
@@ -374,2 +350,29 @@ array, // Array: The array to merge into. | ||
/** | ||
* Get Unix epoch milliseconds from a date. | ||
* @return integer: Epoch milliseconds. | ||
*/ | ||
var getUtcTimestamp = function ( | ||
date // Date: Date object. (Default: now) | ||
) { | ||
var utcMonths = { | ||
Jan: '01', | ||
Feb: '02', | ||
Mar: '03', | ||
Apr: '04', | ||
May: '05', | ||
Jun: '06', | ||
Jul: '07', | ||
Aug: '08', | ||
Sep: '09', | ||
Oct: '10', | ||
Nov: '11', | ||
Dec: '12' | ||
}; | ||
date = date || new Date(); | ||
var utcPattern = /^.*?(\d+) (\w+) (\d+) ([\d:]+).*?$/; | ||
return date.toUTCString().replace(utcPattern, function (a, d, m, y, t) { | ||
return y + '-' + utcMonths[m] + '-' + d + ' ' + t; | ||
}); | ||
}; | ||
/** | ||
* Get a DOM element by its ID (if the argument is an ID). | ||
@@ -1151,3 +1154,6 @@ * If you pass in a DOM element, it just returns it. | ||
if (element) { | ||
element.focus(); | ||
var focusMethod = element.focus; | ||
if (focusMethod) { | ||
focusMethod.call(element); | ||
} | ||
} | ||
@@ -1188,3 +1194,3 @@ }; | ||
var object = usingString ? addTimeout : elementOrString; | ||
var key = usingString ? elementOrString : 'T'; | ||
var key = usingString ? elementOrString : '_TIMEOUT'; | ||
clearTimeout(object[key]); | ||
@@ -1230,3 +1236,3 @@ if (callback) { | ||
} | ||
else if (type == 's') { | ||
else if (options) { | ||
value = options[input.selectedIndex].value; | ||
@@ -1248,6 +1254,7 @@ } | ||
var type = input.type[0]; | ||
var options = input.options; | ||
if (type == 'c' || type == 'r') { | ||
input.checked = value ? true : false; | ||
} | ||
else if (type == 's') { | ||
else if (options) { | ||
var selected = {}; | ||
@@ -1266,3 +1273,3 @@ if (input.multiple) { | ||
value = isArray(value) ? value : [value]; | ||
forEach(input.options, function (option) { | ||
forEach(options, function (option) { | ||
option.selected = !!selected[option.value]; | ||
@@ -1518,15 +1525,2 @@ }); | ||
/** | ||
* Returns a query string generated by serializing an object and joined using a delimiter (defaults to '&') | ||
*/ | ||
var buildQueryString = function ( | ||
object | ||
) { | ||
var queryParams = []; | ||
forIn(object, function(value, key) { | ||
queryParams.push(escape(key) + '=' + escape(value)); | ||
}); | ||
return queryParams.join('&'); | ||
}; | ||
/** | ||
* Returns a lowercase string. | ||
@@ -1570,3 +1564,3 @@ */ | ||
var queryParams = []; | ||
forIn(object, function(value, key) { | ||
forIn(object, function(key, value) { | ||
queryParams.push(escape(key) + '=' + escape(value)); | ||
@@ -1668,3 +1662,90 @@ }); | ||
}; | ||
/** | ||
* Return true if a variable is a date. | ||
*/ | ||
var isDate = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isInstance(value, Date); | ||
}; | ||
/** | ||
* Create a not-strictly-JSON string. | ||
*/ | ||
var stringify = function (data, stack) { | ||
var reserved = /^(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with)$/; | ||
if (data === null) { | ||
data = 'null'; | ||
} | ||
else if (isFunction(data)) { | ||
data = ensureString(data).replace(/^function \(/, 'function('); | ||
} | ||
else if (isDate(data)) { | ||
data = 'new Date(' + getTime(data) + ')'; | ||
} | ||
else if (isObject(data)) { | ||
stack = stack || []; | ||
var isCircular = false; | ||
forEach(stack, function (item, index) { | ||
if (item == data) { | ||
isCircular = true; | ||
} | ||
}); | ||
if (isCircular) { | ||
return null; | ||
} | ||
push(stack, data); | ||
var parts = []; | ||
if (isArray(data)) { | ||
forEach(data, function (value) { | ||
push(parts, stringify(value, stack)); | ||
}); | ||
} | ||
else { | ||
forIn(data, function (key, value) { | ||
if (reserved.test(key)) { | ||
key = '"' + key + '"'; | ||
} | ||
push(parts, key + ':' + stringify(value, stack)); | ||
}); | ||
} | ||
pop(stack); | ||
data = '{' + parts.join(',') + '}'; | ||
} | ||
else if (isString(data) && stack) { | ||
data = '"' + data.replace(/"/g, '\\"') + '"'; | ||
} | ||
else { | ||
data = '' + data; | ||
} | ||
return data; | ||
}; | ||
/** | ||
* Parse JavaScript. | ||
*/ | ||
var parse = function (text) { | ||
if (text[0] == '{') { | ||
try { | ||
var evil = window.eval; // jshint ignore:line | ||
evil('eval.J=' + text); | ||
text = evil.J; | ||
} | ||
catch (e) { | ||
//+env:debug | ||
error('[Jymin] Could not parse JS: "' + text + '"'); | ||
//-env:debug | ||
} | ||
} | ||
return text; | ||
}; | ||
/** | ||
* Execute JavaScript. | ||
*/ | ||
var execute = function (text) { | ||
parse('0;' + text); | ||
}; | ||
/** | ||
* Get the current location host. | ||
@@ -1671,0 +1752,0 @@ */ |
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"main": "chug/chug.js", | ||
@@ -55,5 +55,5 @@ "homepage": "http://lighter.io/jymin", | ||
"coveralls": "2.10.0", | ||
"chug": "0.1.27", | ||
"chug": "0.1.30", | ||
"figlet": "1.0.9" | ||
} | ||
} |
@@ -5,6 +5,15 @@ /** | ||
var doNothing = function () {}; | ||
var globalResponseSuccessHandler = doNothing; | ||
var globalResponseFailureHandler = doNothing; | ||
var responseSuccessHandler = doNothing; | ||
var responseFailureHandler = doNothing; | ||
/** | ||
* Get an XMLHttpRequest object. | ||
*/ | ||
var getXhr = function () { | ||
var Xhr = window.XMLHttpRequest; | ||
var ActiveX = window.ActiveXObject; | ||
return Xhr ? new Xhr() : (ActiveX ? new ActiveX('Microsoft.XMLHTTP') : false); | ||
}; | ||
/** | ||
* Make an AJAX request, and handle it with success or failure. | ||
@@ -17,8 +26,6 @@ * @return boolean: True if AJAX is supported. | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function|: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean|: Whether to evaluate the response as JSON. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
// If the optional body argument is omitted, shuffle it out. | ||
if (isFunction(body)) { | ||
evalJson = onFailure; | ||
onFailure = onSuccess; | ||
@@ -28,12 +35,3 @@ onSuccess = body; | ||
} | ||
var request; | ||
if (window.XMLHttpRequest) { | ||
request = new XMLHttpRequest(); | ||
} | ||
else if (window.ActiveXObject) { | ||
request = new ActiveXObject('Microsoft.XMLHTTP'); // jshint ignore:line | ||
} | ||
else { | ||
return false; | ||
} | ||
var request = getXhr(); | ||
if (request) { | ||
@@ -46,29 +44,9 @@ request.onreadystatechange = function() { | ||
var callback = isSuccess ? | ||
onSuccess || globalResponseSuccessHandler : | ||
onFailure || globalResponseFailureHandler; | ||
var response = request.responseText; | ||
if (evalJson) { | ||
var object; | ||
if (status) { | ||
try { | ||
// Trick UglifyJS into thinking there's no eval. | ||
var evil = window.eval; // jshint ignore:line | ||
evil('eval.J=' + response); | ||
object = evil.J; | ||
} | ||
catch (e) { | ||
//+env:debug,dev | ||
error('Jymin: Could not parse JSON: "' + response + '"'); | ||
//-env:debug,dev | ||
object = {_ERROR: '_BAD_JSON', _TEXT: response}; | ||
} | ||
} | ||
else { | ||
object = {_ERROR: '_OFFLINE'}; | ||
} | ||
object._STATUS = status; | ||
object.request = request; | ||
response = object; | ||
} | ||
callback(response, request); | ||
onSuccess || responseSuccessHandler : | ||
onFailure || responseFailureHandler; | ||
var data = parse(request.responseText); | ||
data._STATUS = status; | ||
data._REQUEST = request; | ||
data = data || {_ERROR: '_OFFLINE'}; | ||
callback(data); | ||
} | ||
@@ -84,13 +62,12 @@ }; | ||
// Record the original request URL. | ||
request.url = url; | ||
request._URL = url; | ||
// TODO: Populate request.query with URL query params. | ||
// If it's a post, record the post body. | ||
if (body) { | ||
request.body = body; | ||
request._BODY = body; | ||
} | ||
// | ||
request._TIME = new Date(); | ||
// Record the time the request was made. | ||
request._TIME = getTime(); | ||
request.send(body || null); | ||
@@ -100,14 +77,1 @@ } | ||
}; | ||
/** | ||
* Request a JSON resource with a given URL. | ||
* @return boolean: True if AJAX is supported. | ||
*/ | ||
var getJson = function ( | ||
url, // string: The URL to request a response from. | ||
body, // object|: Data to post. The method is automagically "POST" if body is truey, otherwise "GET". | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
return getResponse(url, body, onSuccess, onFailure, true); | ||
}; |
@@ -5,4 +5,4 @@ /** | ||
var forEach = function ( | ||
array, // Array*: The array to iterate over. | ||
callback // Function*: The function to call on each item. `callback(item, index, array)` | ||
array, // Array: The array to iterate over. | ||
callback // Function: The function to call on each item. `callback(item, index, array)` | ||
) { | ||
@@ -28,3 +28,3 @@ if (array) { | ||
for (var key in object) { | ||
var result = callback(object[key], key, object); | ||
var result = callback(key, object[key], object); | ||
if (result === false) { | ||
@@ -45,3 +45,3 @@ break; | ||
if (object && decorations) { | ||
forIn(decorations, function (value, key) { | ||
forIn(decorations, function (key, value) { | ||
object[key] = value; | ||
@@ -122,2 +122,14 @@ }); | ||
/** | ||
* Pop an item off an array. | ||
* @return mixed: Popped item. | ||
*/ | ||
var pop = function ( | ||
array // Array: The array to push the item into. | ||
) { | ||
if (isArray(array)) { | ||
return array.pop(); | ||
} | ||
}; | ||
var merge = function ( | ||
@@ -124,0 +136,0 @@ array, // Array: The array to merge into. |
@@ -12,1 +12,28 @@ /** | ||
/** | ||
* Get Unix epoch milliseconds from a date. | ||
* @return integer: Epoch milliseconds. | ||
*/ | ||
var getUtcTimestamp = function ( | ||
date // Date: Date object. (Default: now) | ||
) { | ||
var utcMonths = { | ||
Jan: '01', | ||
Feb: '02', | ||
Mar: '03', | ||
Apr: '04', | ||
May: '05', | ||
Jun: '06', | ||
Jul: '07', | ||
Aug: '08', | ||
Sep: '09', | ||
Oct: '10', | ||
Nov: '11', | ||
Dec: '12' | ||
}; | ||
date = date || new Date(); | ||
var utcPattern = /^.*?(\d+) (\w+) (\d+) ([\d:]+).*?$/; | ||
return date.toUTCString().replace(utcPattern, function (a, d, m, y, t) { | ||
return y + '-' + utcMonths[m] + '-' + d + ' ' + t; | ||
}); | ||
}; |
@@ -236,3 +236,6 @@ /** | ||
if (element) { | ||
element.focus(); | ||
var focusMethod = element.focus; | ||
if (focusMethod) { | ||
focusMethod.call(element); | ||
} | ||
} | ||
@@ -273,3 +276,3 @@ }; | ||
var object = usingString ? addTimeout : elementOrString; | ||
var key = usingString ? elementOrString : 'T'; | ||
var key = usingString ? elementOrString : '_TIMEOUT'; | ||
clearTimeout(object[key]); | ||
@@ -276,0 +279,0 @@ if (callback) { |
@@ -24,3 +24,3 @@ /** | ||
} | ||
else if (type == 's') { | ||
else if (options) { | ||
value = options[input.selectedIndex].value; | ||
@@ -42,6 +42,7 @@ } | ||
var type = input.type[0]; | ||
var options = input.options; | ||
if (type == 'c' || type == 'r') { | ||
input.checked = value ? true : false; | ||
} | ||
else if (type == 's') { | ||
else if (options) { | ||
var selected = {}; | ||
@@ -60,3 +61,3 @@ if (input.multiple) { | ||
value = isArray(value) ? value : [value]; | ||
forEach(input.options, function (option) { | ||
forEach(options, function (option) { | ||
option.selected = !!selected[option.value]; | ||
@@ -63,0 +64,0 @@ }); |
@@ -104,15 +104,2 @@ /** | ||
/** | ||
* Returns a query string generated by serializing an object and joined using a delimiter (defaults to '&') | ||
*/ | ||
var buildQueryString = function ( | ||
object | ||
) { | ||
var queryParams = []; | ||
forIn(object, function(value, key) { | ||
queryParams.push(escape(key) + '=' + escape(value)); | ||
}); | ||
return queryParams.join('&'); | ||
}; | ||
/** | ||
* Returns a lowercase string. | ||
@@ -156,3 +143,3 @@ */ | ||
var queryParams = []; | ||
forIn(object, function(value, key) { | ||
forIn(object, function(key, value) { | ||
queryParams.push(escape(key) + '=' + escape(value)); | ||
@@ -159,0 +146,0 @@ }); |
@@ -83,1 +83,88 @@ /** | ||
}; | ||
/** | ||
* Return true if a variable is a date. | ||
*/ | ||
var isDate = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isInstance(value, Date); | ||
}; | ||
/** | ||
* Create a not-strictly-JSON string. | ||
*/ | ||
var stringify = function (data, stack) { | ||
var reserved = /^(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with)$/; | ||
if (data === null) { | ||
data = 'null'; | ||
} | ||
else if (isFunction(data)) { | ||
data = ensureString(data).replace(/^function \(/, 'function('); | ||
} | ||
else if (isDate(data)) { | ||
data = 'new Date(' + getTime(data) + ')'; | ||
} | ||
else if (isObject(data)) { | ||
stack = stack || []; | ||
var isCircular = false; | ||
forEach(stack, function (item, index) { | ||
if (item == data) { | ||
isCircular = true; | ||
} | ||
}); | ||
if (isCircular) { | ||
return null; | ||
} | ||
push(stack, data); | ||
var parts = []; | ||
if (isArray(data)) { | ||
forEach(data, function (value) { | ||
push(parts, stringify(value, stack)); | ||
}); | ||
} | ||
else { | ||
forIn(data, function (key, value) { | ||
if (reserved.test(key)) { | ||
key = '"' + key + '"'; | ||
} | ||
push(parts, key + ':' + stringify(value, stack)); | ||
}); | ||
} | ||
pop(stack); | ||
data = '{' + parts.join(',') + '}'; | ||
} | ||
else if (isString(data) && stack) { | ||
data = '"' + data.replace(/"/g, '\\"') + '"'; | ||
} | ||
else { | ||
data = '' + data; | ||
} | ||
return data; | ||
}; | ||
/** | ||
* Parse JavaScript. | ||
*/ | ||
var parse = function (text) { | ||
if (text[0] == '{') { | ||
try { | ||
var evil = window.eval; // jshint ignore:line | ||
evil('eval.J=' + text); | ||
text = evil.J; | ||
} | ||
catch (e) { | ||
//+env:debug | ||
error('[Jymin] Could not parse JS: "' + text + '"'); | ||
//-env:debug | ||
} | ||
} | ||
return text; | ||
}; | ||
/** | ||
* Execute JavaScript. | ||
*/ | ||
var execute = function (text) { | ||
parse('0;' + text); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92952
3481