From c9b2cdcc584f45c2033231b2937d41dfc2916b50 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Sat, 19 Jun 2010 21:31:55 -0400 Subject: [PATCH 1/2] added getRight and getLeft --- lib/getFu.js | 13 +++++++++++-- test/getFu-test.js | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/getFu.js b/lib/getFu.js index b1387e7..8790971 100644 --- a/lib/getFu.js +++ b/lib/getFu.js @@ -1,4 +1,5 @@ -var isFu = require("./isFu") +var isFu = require("./isFu"); +var toFu = require("./toFu"); /* @@ -42,6 +43,14 @@ exports.getValues = function( object ) { return values; }; +exports.getRight = function(string, n) { + return string.substring(n, string.length); +}; + +exports.getLeft = function(string, n) { + return toFu.toReverse(toFu.toReverse(string).substring(0, n)); +}; + // If the browser doesn't supply us with indexOf (I'm looking at you, MSIE), // we need this function. Return the position of the first occurence of an // item in an array, or -1 if the item is not included in the array. @@ -54,4 +63,4 @@ exports.getIndex = function( array, item ){ // Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object. exports.getFunctions = function( object ){ // todo add check for iterating through an object and returning an array of all functions (isFunction()) -} \ No newline at end of file +}; \ No newline at end of file diff --git a/test/getFu-test.js b/test/getFu-test.js index 68e8101..086ed4f 100644 --- a/test/getFu-test.js +++ b/test/getFu-test.js @@ -29,7 +29,20 @@ vows.describe('format.js lib/getFu').addBatch({ assert.ok( false, '"' + result.join(", ") + '"' + ' are not the values'); } } + }, + "getRight()": { + topic: "I am a very model of a model major general", + "extracted right of number":function( string ){ + var result = format.getFu.getRight(string, 7); + assert.equal(result, "very model of a model major general"); + } + }, + "getLeft()": { + topic: "I am a very model of a model major general", + "extracted right of number":function( string ){ + var result = format.getFu.getLeft(string, 7); + assert.equal(result, "general"); + } } - -}).export(module); \ No newline at end of file +}).run(); \ No newline at end of file From f86d35b8dc96ed5aae5e8878538034de4b731037 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Sat, 19 Jun 2010 22:03:26 -0400 Subject: [PATCH 2/2] added documentation --- Readme.md | 2 +- build/docs/index.js | 2 +- lib/getFu.js | 46 ++++++++++++++++++++++++++++++++++++++------- lib/isFu.js | 16 ++++++---------- test/getFu-test.js | 20 ++++++++++++++++++-- 5 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Readme.md b/Readme.md index 1f7bcbb..ce7fc65 100644 --- a/Readme.md +++ b/Readme.md @@ -56,7 +56,7 @@ isFu methods will accept anything as an argument and gracefully return toFu methods will accept anything as an argument and aggressively attempt to coerce the value into the type you have specified

getFu - the art of the swift getter

- +

dateTimeFu - the art of space and time

Try out the interactive demo of Date.format()
diff --git a/build/docs/index.js b/build/docs/index.js index 1f7bcbb..ce7fc65 100644 --- a/build/docs/index.js +++ b/build/docs/index.js @@ -56,7 +56,7 @@ isFu methods will accept anything as an argument and gracefully return toFu methods will accept anything as an argument and aggressively attempt to coerce the value into the type you have specified

getFu - the art of the swift getter

- +

dateTimeFu - the art of space and time

Try out the interactive demo of Date.format()
diff --git a/lib/getFu.js b/lib/getFu.js index 8790971..2b3daa5 100644 --- a/lib/getFu.js +++ b/lib/getFu.js @@ -15,18 +15,20 @@ exports.getRight = function( str, n ){ */ - -// returns DOM nodes -exports.getNode = function ( selector ){ - // TODO : add queryselectorall check and jquery check then default to a basic id lookup - return str; -}; +// Docs +var D = {}; exports.getRandom = function(range) { r = Math.floor(Math.random()*range); return r; }; +D.getRandom = { + "example":"getRandom( range );", + "message":"picks a random number from a range", + "code":exports.getRandom.toString() +}; + exports.getKeys = function( object ) { keys = []; for (var key in object) { @@ -35,6 +37,12 @@ exports.getKeys = function( object ) { return keys; }; +D.getKeys = { + "example":"getKeys( object );", + "message":"returns an array of keys for an object", + "code":exports.getKeys.toString() +}; + exports.getValues = function( object ) { values = []; for (var key in object) { @@ -43,23 +51,47 @@ exports.getValues = function( object ) { return values; }; +D.getValues = { + "example":"getValues( object );", + "message":"returns an array of values for an object", + "code":exports.getValues.toString() +}; + exports.getRight = function(string, n) { return string.substring(n, string.length); }; +D.getRight = { + "example":"getRight(string, character_index );", + "message":"returns a substring to the right of character position given", + "code":exports.getRight.toString() +}; + exports.getLeft = function(string, n) { return toFu.toReverse(toFu.toReverse(string).substring(0, n)); }; +D.getLeft = { + "example":"getLeft(string, character_index );", + "message":"returns a substring to the left of character position given", + "code":exports.getLeft.toString() +}; + // If the browser doesn't supply us with indexOf (I'm looking at you, MSIE), // we need this function. Return the position of the first occurence of an // item in an array, or -1 if the item is not included in the array. // Delegates to JavaScript 1.8's native indexOf if available. exports.getIndex = function( array, item ){ - for (var i = 0, l = array.length; i < l; i++) if (array[i] === item) return i; + for (var i = 0, l = array.length; i < l; i++) if (isFu.isEqual(array[i], item)) return i; return -1; }; +D.getIndex = { + "example":"getIndex(array, object );", + "message":"returns index of object's placement in array", + "code":exports.getIndex.toString() +}; + // Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object. exports.getFunctions = function( object ){ // todo add check for iterating through an object and returning an array of all functions (isFunction()) diff --git a/lib/isFu.js b/lib/isFu.js index d0bc804..cdcf9d0 100644 --- a/lib/isFu.js +++ b/lib/isFu.js @@ -118,8 +118,7 @@ exports.isArray = function(obj){ D.isArray = { "example":"isArray( anything );", "message":"checks if anything is array", - "code":exports.isArray.toString(), - "tests":[] + "code":exports.isArray.toString() }; exports.isJSON = function(jsony){ @@ -128,8 +127,7 @@ exports.isJSON = function(jsony){ D.isJSON = { "example":"isJSON( anything );", "message":"checks if anything is a JSON string", - "code":exports.isJSON.toString(), - "tests":[] + "code":exports.isJSON.toString() }; @@ -137,6 +135,7 @@ exports.isNaN = function(obj) { return this.isNumber(obj) && isNaN(obj); }; + exports.isObject = function(objecty){ if(this.isFunction(objecty)) { return false; @@ -149,8 +148,7 @@ exports.isObject = function(objecty){ D.isObject = { "example":"isObject( anything );", "message":"checks if anything is an object", - "code":exports.isObject.toString(), - "tests":[] + "code":exports.isObject.toString() }; @@ -160,8 +158,7 @@ exports.isFunction = function(functiony){ D.isFunction = { "example":"isFunction( anything );", "message":"checks if anything is a function", - "code":exports.isFunction.toString(), - "tests":[] + "code":exports.isFunction.toString() }; exports.isEmpty = function(obj){ @@ -177,8 +174,7 @@ exports.isEmpty = function(obj){ D.isFunction = { "example":"isEmpty( anything );", "message":"checks if anything is empty", - "code":exports.isFunction.toString(), - "tests":[] + "code":exports.isFunction.toString() }; exports.isNode = function(){ diff --git a/test/getFu-test.js b/test/getFu-test.js index 086ed4f..c082d06 100644 --- a/test/getFu-test.js +++ b/test/getFu-test.js @@ -32,17 +32,33 @@ vows.describe('format.js lib/getFu').addBatch({ }, "getRight()": { topic: "I am a very model of a model major general", - "extracted right of number":function( string ){ + "extracted right of string":function( string ){ var result = format.getFu.getRight(string, 7); assert.equal(result, "very model of a model major general"); } }, "getLeft()": { topic: "I am a very model of a model major general", - "extracted right of number":function( string ){ + "extracted left of string":function( string ){ var result = format.getFu.getLeft(string, 7); assert.equal(result, "general"); } + }, + "getIndex()": { + "on complex object": { + topic: ["I", "am", "a", [1,2,3]], + "extracted index if complex object from arra":function( array ){ + var result = format.getFu.getIndex(array, [1,2,3]); + assert.equal(result, 3); + } + }, + "on complex object": { + topic: ["I", "am", "a", [1,2,3]], + "extracted index if simple object from arra":function( array ){ + var result = format.getFu.getIndex(array, "am"); + assert.equal(result, 1); + } + } } }).run(); \ No newline at end of file