Comparing version 0.4.6 to 0.6.0
@@ -0,1 +1,9 @@ | ||
## 0.6 (2015-02-22) | ||
Features: | ||
- Use ES6, remove any function already in ES6 (@mikemaccana) | ||
- Remove requirejs support, just use CommonJS (@mikemaccana) | ||
- Remove DOM functions, these will soon appear in a separate library | ||
## 0.4.4 (2014-05-29) | ||
@@ -34,2 +42,2 @@ | ||
- Initial release | ||
- Initial release |
795
index.js
@@ -1,512 +0,403 @@ | ||
// Agave.JS | ||
// I'm a UMD module (works in RequireJS and CommonJS-like environments) | ||
// See https://github.com/umdjs | ||
(function (global, factory) { | ||
if (typeof exports === 'object') { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like enviroments that support module.exports, | ||
// like Node. | ||
module.exports = factory(global); | ||
} else if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define(factory); | ||
} else { | ||
// Browser globals (global is window) | ||
global.returnExports = factory(); | ||
} | ||
}(this, function(global) { | ||
require('es6-shim'); | ||
var enabledPrefixes = {}; // Only allow agave to be enabled once per prefix | ||
var enabledPrefixes = {}; // Only allow agave to be enabled once per prefix | ||
// Extend objects with Agave methods, using the prefix provided. | ||
var enable = function(prefix){ | ||
// 'this' would be 'window' in browser enviroments. | ||
// Must come before strict mode, otherwise requirejs breaks. | ||
// Extend objects with Agave methods, using the prefix provided. | ||
var enable = function(prefix){ | ||
"use strict"; | ||
var global = this; | ||
prefix = prefix || ''; | ||
"use strict"; | ||
if ( enabledPrefixes[prefix] ) { | ||
return; | ||
} | ||
prefix = prefix || ''; | ||
var SECONDS = 1000; | ||
var MINUTES = 60 * SECONDS; | ||
var HOURS = 60 * MINUTES; | ||
var DAYS = 24 * HOURS; | ||
var WEEKS = 7 * DAYS; | ||
if ( enabledPrefixes[prefix] ) { | ||
return; | ||
} | ||
// object.getKeys() returns an array of keys | ||
var getKeys = function(){ | ||
return Object.keys(this); | ||
}; | ||
var SECONDS = 1000; | ||
var MINUTES = 60 * SECONDS; | ||
var HOURS = 60 * MINUTES; | ||
var DAYS = 24 * HOURS; | ||
var WEEKS = 7 * DAYS; | ||
// object.getSize() returns the number of properties in the object | ||
var getSize = function() { | ||
return Object.keys(this).length; | ||
}; | ||
// object.getKeys() returns an array of keys | ||
var getKeys = function(){ | ||
return Object.keys(this); | ||
}; | ||
// string.reverse() | ||
var reverse = function() { | ||
return this.split("").reverse().join(""); | ||
}; | ||
// object.getSize() returns the number of properties in the object | ||
var getSize = function() { | ||
return Object.keys(this).length; | ||
}; | ||
// string.leftStrip(stripChars) returns the string with the leading chars removed | ||
var leftStrip = function(stripChars) { | ||
var result = this; | ||
while ( true ) { | ||
// Note result could be zero characters | ||
if ( ! stripChars.includes(result.charAt(0)) || ! result) { | ||
return result; | ||
} else { | ||
result = result.slice(1); | ||
} | ||
} | ||
}; | ||
// string.reverse() | ||
var reverse = function() { | ||
return this.split("").reverse().join(""); | ||
}; | ||
// string.rightStrip(stripChars) returns the string with the trailing chars removed | ||
var rightStrip = function(stripChars) { | ||
return this[prefix+'reverse']()[prefix+'leftStrip'](stripChars)[prefix+'reverse'](); | ||
}; | ||
// string.leftStrip(stripChars) returns the string with the leading chars removed | ||
var leftStrip = function(stripChars) { | ||
var result = this; | ||
while ( true ) { | ||
// Note result could be zero characters | ||
if ( ! stripChars[prefix+'contains'](result.charAt(0)) || ! result) { | ||
return result; | ||
// string.strip(stripChars) returns the string with the leading and trailing chars removed | ||
var strip = function(stripChars) { | ||
return this[prefix+'leftStrip'](stripChars)[prefix+'rightStrip'](stripChars); | ||
}; | ||
// object.getPath - get the value of the nested keys provided in the object. | ||
// If any are missing, return undefined. Used for checking JSON results. | ||
var getPath = function(pathItems) { | ||
var currentObject = this; | ||
var delim = '/'; | ||
var result; | ||
var stillChecking = true; | ||
// Handle Unix style paths | ||
if ( typeof(pathItems) === 'string' ) { | ||
pathItems = pathItems[prefix+'strip'](delim).split(delim); | ||
} | ||
pathItems.forEach( function(pathItem) { | ||
if ( stillChecking ) { | ||
if ( ( currentObject === null ) || ( ! currentObject.hasOwnProperty(pathItem) ) ) { | ||
result = undefined; | ||
stillChecking = false; | ||
} else { | ||
result = result.slice(1); | ||
result = currentObject[pathItem]; | ||
currentObject = currentObject[pathItem]; | ||
} | ||
} | ||
}; | ||
}); | ||
return result; | ||
}; | ||
// string.rightStrip(stripChars) returns the string with the trailing chars removed | ||
var rightStrip = function(stripChars) { | ||
return this[prefix+'reverse']()[prefix+'leftStrip'](stripChars)[prefix+'reverse'](); | ||
}; | ||
// object.extent(object) adds the keys/values from the newObject provided | ||
var objectExtend = function(newObject) { | ||
for ( var key in newObject ) { | ||
this[key] = newObject[key]; | ||
} | ||
return this; | ||
}; | ||
// string.strip(stripChars) returns the string with the leading and trailing chars removed | ||
var strip = function(stripChars) { | ||
return this[prefix+'leftStrip'](stripChars)[prefix+'rightStrip'](stripChars); | ||
}; | ||
// array.findItem(testFunction) returns the first item that matches the testFunction | ||
var findItem = function(testFunction){ | ||
var lastIndex; | ||
var found = this.some(function(item, index) { | ||
lastIndex = index; | ||
return testFunction(item); | ||
}); | ||
if ( found ) { | ||
return this[lastIndex]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
// object.getPath - get the value of the nested keys provided in the object. | ||
// If any are missing, return undefined. Used for checking JSON results. | ||
var getPath = function(pathItems) { | ||
var currentObject = this; | ||
var delim = '/'; | ||
var result; | ||
var stillChecking = true; | ||
// Handle Unix style paths | ||
if ( typeof(pathItems) === 'string' ) { | ||
pathItems = pathItems[prefix+'strip'](delim).split(delim); | ||
} | ||
pathItems.forEach( function(pathItem) { | ||
if ( stillChecking ) { | ||
if ( ( currentObject === null ) || ( ! currentObject.hasOwnProperty(pathItem) ) ) { | ||
result = undefined; | ||
stillChecking = false; | ||
} else { | ||
result = currentObject[pathItem]; | ||
currentObject = currentObject[pathItem]; | ||
} | ||
} | ||
}); | ||
return result; | ||
}; | ||
// object.extent(object) adds the keys/values from the newObject provided | ||
var objectExtend = function(newObject) { | ||
for ( var key in newObject ) { | ||
this[key] = newObject[key]; | ||
} | ||
return this; | ||
}; | ||
// array.findItem(testFunction) returns the first item that matches the testFunction | ||
var findItem = function(testFunction){ | ||
var lastIndex; | ||
var found = this.some(function(item, index) { | ||
lastIndex = index; | ||
return testFunction(item); | ||
}); | ||
if ( found ) { | ||
return this[lastIndex]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
// Run after it hasn't been invoked for 'wait' ms. | ||
// Useful to stop repeated calls to a function overlapping each other (sometimes called 'bouncing') | ||
var throttle = function(wait, immediate) { | ||
var timeoutID; | ||
var originalFunction = this; | ||
return function() { | ||
var context = this; | ||
var delayedFunction = function() { | ||
timeoutID = null; | ||
if ( ! immediate ) { | ||
originalFunction.apply(context, arguments); | ||
} | ||
}; | ||
var callNow = immediate && ! timeoutID; | ||
clearTimeout(timeoutID); | ||
timeoutID = setTimeout(delayedFunction, wait); | ||
if (callNow) { | ||
// Run after it hasn't been invoked for 'wait' ms. | ||
// Useful to stop repeated calls to a function overlapping each other (sometimes called 'bouncing') | ||
var throttle = function(wait, immediate) { | ||
var timeoutID; | ||
var originalFunction = this; | ||
return function() { | ||
var context = this; | ||
var delayedFunction = function() { | ||
timeoutID = null; | ||
if ( ! immediate ) { | ||
originalFunction.apply(context, arguments); | ||
} | ||
}; | ||
var callNow = immediate && ! timeoutID; | ||
clearTimeout(timeoutID); | ||
timeoutID = setTimeout(delayedFunction, wait); | ||
if (callNow) { | ||
originalFunction.apply(context, arguments); | ||
} | ||
}; | ||
}; | ||
// Run repeatedly | ||
var functionRepeat = function(first, second, third){ | ||
var args, interval, leadingEdge; | ||
if ( arguments.length === 2 ) { | ||
args = []; | ||
interval = first; | ||
leadingEdge = second; | ||
} else { | ||
args = first; | ||
interval = second; | ||
leadingEdge = third; | ||
} | ||
if ( leadingEdge ) { | ||
this.apply(null, args); | ||
} | ||
return setInterval(function(){ | ||
this.apply(null, args); | ||
}.bind(this), interval) | ||
// Run repeatedly | ||
var functionRepeat = function(first, second, third){ | ||
var args, interval, leadingEdge; | ||
if ( arguments.length === 2 ) { | ||
args = []; | ||
interval = first; | ||
leadingEdge = second; | ||
} else { | ||
args = first; | ||
interval = second; | ||
leadingEdge = third; | ||
} | ||
if ( leadingEdge ) { | ||
this.apply(null, args); | ||
} | ||
return setInterval(function(){ | ||
this.apply(null, args); | ||
}.bind(this), interval); | ||
}; | ||
// string.endsWith(suffix) returns true if string ends with the suffix | ||
var endsWith = function(suffix) { | ||
return this.indexOf(suffix, this.length - suffix.length) !== -1; | ||
}; | ||
// array.contains(item) returns true if an array contains an item | ||
var contains = function(item){ | ||
return ( this.indexOf(item) >= 0 ); | ||
}; | ||
// string.endsWith(prefix) returns true if string ends with the prefix | ||
var startsWith = function(prefix){ | ||
return this.slice(0, prefix.length) === prefix; | ||
}; | ||
// Extend an array with another array. | ||
// Cleverness alert: since .apply() accepts an array of args, we use the newArray as all the args to push() | ||
var arrayExtend = function(newArray) { | ||
Array.prototype.push.apply(this, newArray); | ||
return this; | ||
}; | ||
// array.contains(item) returns true if an array contains an item | ||
// string.contains(substring) returns true if a string contains a substring | ||
var contains = function(item){ | ||
return ( this.indexOf(item) >= 0 ); | ||
}; | ||
// Extend an array with another array. | ||
// Cleverness alert: since .apply() accepts an array of args, we use the newArray as all the args to push() | ||
var arrayExtend = function(newArray) { | ||
Array.prototype.push.apply(this, newArray); | ||
return this; | ||
}; | ||
// string.repeat() repeat a string 'times' times. Borrowed from ES6 shim at https://github.com/paulmillr/es6-shim | ||
var repeat = function(times) { | ||
if (times < 1) return ''; | ||
if (times % 2) return this[prefix+'repeat'](times - 1) + this; | ||
var half = this[prefix+'repeat'](times / 2); | ||
return half + half; | ||
}; | ||
// string.toHash() return a hashed value of a string | ||
// From http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery | ||
var toHash = function(){ | ||
var hash = 0, | ||
length = this.length, | ||
char; | ||
if ( ! length ) { | ||
return hash; | ||
} | ||
for (var index = 0; index < length; index++) { | ||
char = this.charCodeAt(index); | ||
hash = ((hash<<5)-hash)+char; | ||
hash |= 0; // Convert to 32bit integer | ||
} | ||
// string.toHash() return a hashed value of a string | ||
// From http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery | ||
var toHash = function(){ | ||
var hash = 0, | ||
length = this.length, | ||
char; | ||
if ( ! length ) { | ||
return hash; | ||
}; | ||
} | ||
for (var index = 0; index < length; index++) { | ||
char = this.charCodeAt(index); | ||
hash = ((hash<<5)-hash)+char; | ||
hash |= 0; // Convert to 32bit integer | ||
} | ||
return hash; | ||
}; | ||
// Clone an object recursively | ||
var clone = function() { | ||
var newObj = (this instanceof Array) ? [] : {}; | ||
for (var key in this) { | ||
if (this[key] && typeof this[key] == "object") { | ||
newObj[key] = this[key][prefix+'clone'](); | ||
} else { | ||
newObj[key] = this[key]; | ||
} | ||
// Clone an object recursively | ||
var clone = function() { | ||
var newObj = (this instanceof Array) ? [] : {}; | ||
for (var key in this) { | ||
if (this[key] && typeof this[key] == "object") { | ||
newObj[key] = this[key][prefix+'clone'](); | ||
} else { | ||
newObj[key] = this[key]; | ||
} | ||
return newObj; | ||
}; | ||
} | ||
return newObj; | ||
}; | ||
// compare an object with another object | ||
var compare = function(otherObject){ | ||
var hashObject = function(object){ | ||
return JSON.stringify(object)[prefix+'toHash'](); | ||
}; | ||
return ( hashObject(this) === hashObject(otherObject) ); | ||
// compare an object with another object | ||
var compare = function(otherObject){ | ||
var hashObject = function(object){ | ||
return JSON.stringify(object)[prefix+'toHash'](); | ||
}; | ||
return ( hashObject(this) === hashObject(otherObject) ); | ||
}; | ||
// Iterate over an objects keys | ||
// Unlike a regular for ( var key in object ) | ||
// an additional scope is created, which avoids last-item looping probs | ||
var objectForEach = function(callback){ | ||
for ( var key in this ) { | ||
callback(key, this[key]); | ||
} | ||
}; | ||
// Iterate over an objects keys | ||
// Unlike a regular for ( var key in object ) | ||
// an additional scope is created, which avoids last-item looping probs | ||
var objectForEach = function(callback){ | ||
for ( var key in this ) { | ||
callback(key, this[key]); | ||
} | ||
}; | ||
var arrayClone = function(){ | ||
return this.slice(); | ||
}; | ||
var arrayClone = function(){ | ||
return this.slice(); | ||
}; | ||
// Array toNodeList converts arrays to NodeLists | ||
var toNodeList = function(){ | ||
var fragment = document.createDocumentFragment(); | ||
this.forEach(function(item){ | ||
fragment.appendChild(item); | ||
}); | ||
return fragment.childNodes; | ||
}; | ||
// Array remove removes an item from an array, if it exists | ||
var arrayRemove = function(member){ | ||
var index = this.indexOf(member); | ||
if (index !== -1 ) { | ||
this.splice(index, 1); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
// Array remove removes an item from an array, if it exists | ||
var arrayRemove = function (member){ | ||
var index = this.indexOf(member); | ||
if (index !== -1 ) { | ||
this.splice(index, 1); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var arrayFirst= function(count){ | ||
if ( ! count ) { | ||
return this[0]; | ||
} else { | ||
return this.slice(Math.max(arr.length - count, 1)); | ||
} | ||
}; | ||
// Convert Number to (function name). +ensures type returned is still Number | ||
var seconds = function() { | ||
return +this * SECONDS; | ||
}; | ||
var minutes = function() { | ||
return +this * MINUTES; | ||
}; | ||
var hours = function() { | ||
return +this * HOURS; | ||
}; | ||
var days = function() { | ||
return +this * DAYS; | ||
}; | ||
var weeks = function() { | ||
return +this * WEEKS; | ||
}; | ||
var arrayLast = function(count){ | ||
if ( ! count ) { | ||
return this[this.length - 1]; | ||
} else { | ||
return this.slice(Math.max(this.length - count, 1)); | ||
} | ||
}; | ||
// Helper function for before() and after() | ||
var getTimeOrNow = function(date) { | ||
return (date || new Date()).getTime(); | ||
}; | ||
// Convert Number to (function name). +ensures type returned is still Number | ||
var seconds = function() { | ||
return +this * SECONDS; | ||
}; | ||
var minutes = function() { | ||
return +this * MINUTES; | ||
}; | ||
var hours = function() { | ||
return +this * HOURS; | ||
}; | ||
var days = function() { | ||
return +this * DAYS; | ||
}; | ||
var weeks = function() { | ||
return +this * WEEKS; | ||
}; | ||
// Return Number of seconds to time delta from date (or now if not specified) | ||
var before = function(date) { | ||
var time = getTimeOrNow(date); | ||
return new Date(time-(+this)); | ||
}; | ||
// Helper function for before() and after() | ||
var getTimeOrNow = function(date) { | ||
return (date || new Date()).getTime(); | ||
}; | ||
// Return Number of seconds to time delta after date (or now if not specified) | ||
var after = function(date) { | ||
var time = getTimeOrNow(date); | ||
return new Date(time+(+this)); | ||
}; | ||
// Return Number of seconds to time delta from date (or now if not specified) | ||
var before = function(date) { | ||
var time = getTimeOrNow(date); | ||
return new Date(time-(+this)); | ||
}; | ||
// Round Number | ||
var round = function () { | ||
return Math.round(this); | ||
}; | ||
// Return Number of seconds to time delta after date (or now if not specified) | ||
var after = function(date) { | ||
var time = getTimeOrNow(date); | ||
return new Date(time+(+this)); | ||
}; | ||
var ceil = function () { | ||
return Math.ceil(this); | ||
}; | ||
// Round Number | ||
var round = function () { | ||
return Math.round(this); | ||
}; | ||
var floor = function () { | ||
return Math.floor(this); | ||
}; | ||
var ceil = function () { | ||
return Math.ceil(this); | ||
}; | ||
var abs = function () { | ||
return Math.abs(this); | ||
}; | ||
var floor = function () { | ||
return Math.floor(this); | ||
}; | ||
var pow = function (exp) { | ||
return Math.pow(this, exp); | ||
}; | ||
var abs = function () { | ||
return Math.abs(this); | ||
}; | ||
// Add a new element as a child of this element | ||
var createChild = function(name, attributes, text) { | ||
var newElement = document.createElement(name); | ||
if ( attributes ) { | ||
for (var attribute in attributes) { | ||
newElement.setAttribute(attribute, attributes[attribute]); | ||
} | ||
} | ||
if ( text ) { | ||
newElement.textContent = text; | ||
} | ||
return this.appendChild(newElement); | ||
}; | ||
var pow = function (exp) { | ||
return Math.pow(this, exp); | ||
}; | ||
// Apply the CSS styles | ||
var applyStyles = function(styles) { | ||
for ( var style in styles ) { | ||
this.style[style] = styles[style]; | ||
} | ||
return this; | ||
var kind = function(item) { | ||
var getPrototype = function(item) { | ||
return Object.prototype.toString.call(item).slice(8, -1); | ||
}; | ||
// Toggle a class (considering replacing this with IE9 only element.classList.toggle) | ||
var toggleClass = function(className) { | ||
if ( this.classList.contains(className) ) { | ||
this.classList.remove(className); | ||
var kind, Undefined; | ||
if (item === null ) { | ||
kind = 'null'; | ||
} else { | ||
if ( item === Undefined ) { | ||
kind = 'undefined'; | ||
} else { | ||
this.classList.add(className); | ||
} | ||
return this; | ||
}; | ||
// Return nodeList of an elements parent elements from closest to farthest | ||
var ancestorNodes = function(selector) { | ||
var ancestors = []; | ||
var parent = this.parentNode; | ||
// While parents are 'element' type nodes | ||
// See https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType | ||
while ( parent && parent.nodeType && parent.nodeType === 1 ) { | ||
if ( selector ) { | ||
if ( parent.matches(selector) ) { | ||
ancestors.push(parent); | ||
} | ||
var prototype = getPrototype(item); | ||
if ( ( prototype === 'Number' ) && isNaN(item) ) { | ||
kind = 'NaN'; | ||
} else { | ||
ancestors.push(parent); | ||
kind = prototype; | ||
} | ||
parent = parent.parentNode; | ||
} | ||
// Return a NodeList to be consistent with childNodes | ||
return ancestors[prefix+'toNodeList'](); | ||
}; | ||
// Return index of node under its parents. Eg, if you're the fourth child, return 3. | ||
var getParentIndex = function() { | ||
return Array.prototype.indexOf.call(this.parentNode.children, this); | ||
} | ||
return kind; | ||
}; | ||
var kind = function(item) { | ||
var getPrototype = function(item) { | ||
return Object.prototype.toString.call(item).slice(8, -1); | ||
}; | ||
var kind, Undefined; | ||
if (item === null ) { | ||
kind = 'null'; | ||
} else { | ||
if ( item === Undefined ) { | ||
kind = 'undefined'; | ||
} else { | ||
var prototype = getPrototype(item); | ||
if ( ( prototype === 'Number' ) && isNaN(item) ) { | ||
kind = 'NaN'; | ||
} else { | ||
kind = prototype; | ||
} | ||
} | ||
} | ||
return kind; | ||
}; | ||
// Polyfill if Element.prototype.matches doesn't exist. | ||
var prefixedMatchesMethod = ( ! global.Element || Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector || Element.prototype.oMatchesSelector); | ||
// Polyfill if Element.prototype.matches doesn't exist. | ||
var prefixedMatchesMethod = ( ! global.Element || Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector || Element.prototype.oMatchesSelector); | ||
// Add method as a non-enumerable property on obj with the name methodName | ||
var addMethod = function( global, objectName, prefix, methodName, method) { | ||
var objectToExtend = global[objectName]; | ||
methodName = prefix ? prefix+methodName: methodName; | ||
// Check - NodeLists and Elements don't always exist on all JS implementations | ||
if ( objectToExtend ) { | ||
// Don't add if the method already exists | ||
if ( ! objectToExtend.prototype.hasOwnProperty(methodName) ) { | ||
Object.defineProperty( objectToExtend.prototype, methodName, { | ||
value: method, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
} | ||
// Add method as a non-enumerable property on obj with the name methodName | ||
var addMethod = function( global, objectName, prefix, methodName, method) { | ||
var objectToExtend = global[objectName]; | ||
methodName = prefix ? prefix+methodName: methodName; | ||
// Check - NodeLists and Elements don't always exist on all JS implementations | ||
if ( objectToExtend ) { | ||
// Don't add if the method already exists | ||
if ( ! objectToExtend.prototype.hasOwnProperty(methodName) ) { | ||
Object.defineProperty( objectToExtend.prototype, methodName, { | ||
value: method, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
// There's not always a 1:1 match of functions to method names. Eg, some objects share methods, | ||
// others re-use inbuilt methods from other objects. | ||
var newMethods = { | ||
'Array':{ | ||
'findItem':findItem, | ||
'extend':arrayExtend, | ||
'contains':contains, | ||
'clone':arrayClone, | ||
'toNodeList':toNodeList, | ||
'remove':arrayRemove | ||
}, | ||
'Object':{ | ||
'getKeys':getKeys, | ||
'getSize':getSize, | ||
'getPath':getPath, | ||
'clone':clone, | ||
'forEach':objectForEach, | ||
'extend':objectExtend, | ||
'compare':compare | ||
}, | ||
'String':{ | ||
'endsWith':endsWith, | ||
'startsWith':startsWith, | ||
'repeat':repeat, | ||
'reverse':reverse, | ||
'leftStrip':leftStrip, | ||
'rightStrip':rightStrip, | ||
'strip':strip, | ||
'contains':contains, | ||
'toHash':toHash, | ||
'forEach':Array.prototype.forEach // Strings and NodeLists don't have .forEach() standard but the one from Array works fine | ||
}, | ||
'Function':{ | ||
'throttle':throttle, | ||
'repeat':functionRepeat | ||
}, | ||
'Number':{ | ||
'seconds':seconds, | ||
'minutes':minutes, | ||
'hours':hours, | ||
'days':days, | ||
'weeks':weeks, | ||
'before':before, | ||
'after':after, | ||
'round':round, | ||
'ceil':ceil, | ||
'floor':floor, | ||
'abs':abs, | ||
'pow':pow | ||
}, | ||
'Element':{ | ||
'createChild':createChild, | ||
'ancestorNodes':ancestorNodes, | ||
'matches':prefixedMatchesMethod, | ||
'applyStyles':applyStyles, | ||
'toggleClass':toggleClass, | ||
'getParentIndex':getParentIndex | ||
}, | ||
'NodeList':{ | ||
'forEach':Array.prototype.forEach, | ||
'reverse':Array.prototype.reverse | ||
} | ||
}; | ||
for ( var objectName in newMethods ) { | ||
for ( var methodName in newMethods[objectName] ) { | ||
addMethod(global, objectName, prefix, methodName, newMethods[objectName][methodName]); | ||
} | ||
// There's not always a 1:1 match of functions to method names. Eg, some objects share methods, | ||
// others re-use inbuilt methods from other objects. | ||
var newMethods = { | ||
'Array':{ | ||
'findItem':findItem, | ||
'extend':arrayExtend, | ||
'contains':contains, | ||
'clone':arrayClone, | ||
'remove':arrayRemove, | ||
'first':arrayFirst, | ||
'last':arrayLast | ||
}, | ||
'Object':{ | ||
'getKeys':getKeys, | ||
'getSize':getSize, | ||
'getPath':getPath, | ||
'clone':clone, | ||
'forEach':objectForEach, | ||
'extend':objectExtend, | ||
'compare':compare | ||
}, | ||
'String':{ | ||
'reverse':reverse, | ||
'leftStrip':leftStrip, | ||
'rightStrip':rightStrip, | ||
'strip':strip, | ||
'toHash':toHash, | ||
'forEach':Array.prototype.forEach // Strings and NodeLists don't have .forEach() standard but the one from Array works fine | ||
}, | ||
'Function':{ | ||
'throttle':throttle, | ||
'repeat':functionRepeat | ||
}, | ||
'Number':{ | ||
'seconds':seconds, | ||
'minutes':minutes, | ||
'hours':hours, | ||
'days':days, | ||
'weeks':weeks, | ||
'before':before, | ||
'after':after, | ||
'round':round, | ||
'ceil':ceil, | ||
'floor':floor, | ||
'abs':abs, | ||
'pow':pow | ||
} | ||
}; | ||
for ( var objectName in newMethods ) { | ||
for ( var methodName in newMethods[objectName] ) { | ||
addMethod(global, objectName, prefix, methodName, newMethods[objectName][methodName]); | ||
} | ||
} | ||
// Add a function to the global | ||
var addGlobal = function( global, globalName, prefix, globalFunction) { | ||
globalName = prefix ? prefix+globalName: globalName; | ||
// Don't add if the global already exists | ||
if ( ! global.hasOwnProperty(globalName) ) { | ||
global[globalName] = globalFunction; | ||
} | ||
}; | ||
addGlobal(global, 'kind', prefix, kind); | ||
// Add a function to the global | ||
var addGlobal = function( global, globalName, prefix, globalFunction) { | ||
globalName = prefix ? prefix+globalName: globalName; | ||
// Don't add if the global already exists | ||
if ( ! global.hasOwnProperty(globalName) ) { | ||
global[globalName] = globalFunction; | ||
} | ||
}; | ||
addGlobal(global, 'kind', prefix, kind); | ||
enabledPrefixes[prefix] = true; | ||
}.bind(); | ||
enabledPrefixes[prefix] = true; | ||
}.bind(); | ||
// Just return a value to define the module export. | ||
// This example returns an object, but the module | ||
// can return a function as the exported value. | ||
return { | ||
enable:enable | ||
}; | ||
})); | ||
module.exports = { | ||
enable: enable | ||
} |
{ | ||
"name": "agave", | ||
"version": "0.4.6", | ||
"version": "0.6.0", | ||
"author": "Mike MacCana <mike.maccana@gmail.com>", | ||
"description": "Safely extends JS with helpful, intuitive methods.", | ||
"description": "Cleaner, simpler JavaScript for ES6", | ||
"scripts": { | ||
@@ -21,6 +21,6 @@ "test": "mocha" | ||
], | ||
"dependencies" : {}, | ||
"dependencies": { | ||
"es6-shim": "^0.25.3" | ||
}, | ||
"devDependencies": { | ||
"jsdom": "==0.10.5", | ||
"requirejs": "==2.1.11", | ||
"mocha": "==1.18.2" | ||
@@ -33,2 +33,1 @@ }, | ||
} | ||
![Agave JS](http://agavejs.org/images/agave.png) | ||
## Cleaner, simpler JavaScript for ES5 environments | ||
## Cleaner, simpler JavaScript for ES6 environments | ||
@@ -9,2 +9,2 @@ [![Build Status](https://secure.travis-ci.org/mikemaccana/agave.png?branch=master)](https://travis-ci.org/mikemaccana/agave) | ||
[For full documentation, visit agavejs.org](http://agavejs.org) | ||
[For full documentation, visit agavejs.org](http://agavejs.org) |
@@ -14,3 +14,2 @@ // Tests. Mocha/assert style. See | ||
var assert = require('assert'); | ||
var jsdom = require('jsdom'); | ||
var agave = require('../index.js'); | ||
@@ -33,30 +32,2 @@ | ||
// Set up a global.document with a DOM in the same way a browser has | ||
var setupDOM = function(documentText) { | ||
var document = jsdom.jsdom(documentText, null, { | ||
features: { | ||
QuerySelector: true | ||
} | ||
}); | ||
var window = document.createWindow(); | ||
['Element','NodeList','document'].forEach(function(obj){ | ||
global[obj] = window[obj]; | ||
}); | ||
}; | ||
var mockHTML = ' \ | ||
<html> \ | ||
<body> \ | ||
<article> \ | ||
<heading>Sample document</heading> \ | ||
<author></author> \ | ||
<p>Carles portland banh mi lomo twee.</p> \ | ||
<p>Narwhal bicycle rights keffiyeh beard.</p> \ | ||
<p>Pork belly beard pop-up kale chips.</p> \ | ||
</article> \ | ||
</body> \ | ||
</html>'; | ||
setupDOM(mockHTML); | ||
agave.enable('av'); | ||
@@ -79,32 +50,2 @@ | ||
suite('String.contains', function(){ | ||
test('checks for the substring accurately', function(){ | ||
assert('elephantine'.avcontains('tin') ); | ||
}); | ||
test('handles missing substrings accurately', function(){ | ||
assert( ! 'elephantine'.avcontains('zam') ); | ||
}); | ||
}); | ||
suite('String.endsWith', function(){ | ||
test('works if the string actually ends with the suffix', function(){ | ||
assert('Hello world'.avendsWith('world')); | ||
}); | ||
test('handles trying to check if something ends in something larger than itself', function(){ | ||
assert.equal('world'.avendsWith('Hello world'), false); | ||
}); | ||
}); | ||
suite('String.startsWith', function(){ | ||
test('works if the string actually starts with the prefix', function(){ | ||
assert('Hello world'.avstartsWith('Hello')); | ||
}); | ||
}); | ||
suite('String.repeat', function(){ | ||
test('repeats strings accurately', function(){ | ||
assert.equal('Hello world'.avrepeat(3), 'Hello worldHello worldHello world'); | ||
}); | ||
}); | ||
suite('String.reverse', function(){ | ||
@@ -258,3 +199,2 @@ test('reverses strings accurately', function(){ | ||
test('accurately identifies similar objects', function(){ | ||
var heading = document.querySelector('heading'); | ||
var identicalObject = { | ||
@@ -277,3 +217,2 @@ foo: 'bar', | ||
test('accurately identifies different objects', function(){ | ||
var heading = document.querySelector('heading'); | ||
var differentObject = { | ||
@@ -330,3 +269,2 @@ foo: 'bar', | ||
var increment = function(){ | ||
console.log('RUNNING!') | ||
count += 1 | ||
@@ -397,3 +335,3 @@ if ( count === 3 ) { | ||
suite('Agave really doesn\'t affect for loops', function(){ | ||
suite('Agave doesn\'t affect for loops', function(){ | ||
it ('doesn\'t. really', function(){ | ||
@@ -406,35 +344,2 @@ for ( var key in mockObject ) { | ||
suite('Element.createChild', function(){ | ||
var sillyText = 'ethical messenger bag'; | ||
var article = document.querySelector('article'); | ||
article.avcreateChild('p',{'id':'testpara'},sillyText); | ||
test('creates children with the specified attributes', function(){ | ||
var paraCount = document.querySelector('#testpara'); | ||
assert(paraCount); | ||
}); | ||
test('creates children with the specified text', function(){ | ||
assert(document.querySelector('#testpara').textContent === sillyText ); | ||
}); | ||
}); | ||
suite('Element.getParentIndex', function(){ | ||
var article = document.createElement('article'); | ||
['first', 'second', 'third'].forEach(function(item){ | ||
var p = document.createElement("p"); | ||
article.appendChild(p); | ||
}) | ||
test('creates children with the specified attributes', function(){ | ||
var thirdChild = article.querySelectorAll('p')[2]; | ||
assert(thirdChild.avgetParentIndex() === 2); | ||
}); | ||
}); | ||
suite('Element.applyStyles', function(){ | ||
test('styles elements', function(){ | ||
var heading = document.querySelector('heading'); | ||
heading.avapplyStyles({'font-size':'18em'}); | ||
assert.equal(heading.style['font-size'], '18em'); | ||
}); | ||
}); | ||
suite('kind', function(){ | ||
@@ -441,0 +346,0 @@ test('shows number-like things as numbers', function(){ |
Sorry, the diff of this file is not supported yet
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
1
10
25648
1
714
+ Addedes6-shim@^0.25.3
+ Addedes6-shim@0.25.3(transitive)