Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: wayoutmind/crackle
base: 5f9ec40fd8
...
head fork: wayoutmind/crackle
compare: 29f3429344
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 29, 2012
@wayoutmind Update tagline
b1739cd
@wayoutmind Document API methods
b9eba3d
@wayoutmind Add value and object array predicate functions
- Test that values in Array are satisfactory
- Test that objects in Array are satisfactory
29f3429
Showing with 197 additions and 13 deletions.
  1. +1 −1  LICENSE
  2. +83 −1 README.md
  3. +77 −1 crackle.js
  4. +36 −10 test/crackle.test.js
View
2  LICENSE
@@ -1,4 +1,4 @@
-Crackle: JavaScript comparator, equality, and utility functions
+Crackle: JavaScript predicate, comparator, equality, and utility functions
Copyright (c) 2012 Fredrick Galoso http://fredrickgaloso.me
Permission is hereby granted, free of charge, to any person obtaining
View
84 README.md
@@ -1,4 +1,86 @@
Crackle
=======
-JavaScript predicate, comparator, equality, and utility functions
+> JavaScript predicate, comparator, equality, and utility functions
+
+Crackle is collection of useful JavaScript functions for both the browser and server.
+Specifically it focuses on collection predicate operations, such as "all or nothing" verification, and also makes working with sets of jQuery Objects easier.
+
+##Getting started
+
+###Node.js
+
+Install Crackle with NPM (Node Package Manager).
+
+```
+$ npm install crackle
+```
+
+```javascript
+var Crackle = require('crackle');
+```
+
+###Web browser
+
+Download and include [crackle.min.js](https://raw.github.com/wayoutmind/crackle/master/crackle.min.js).
+
+```html
+ <script src="crackle.min.js" type="text/javascript"></script>
+ <script type="text/javascript" charset="utf-8">
+ Crackle.difference([1, 2, 3]).from([1, 2, 3, 4, 5]);
+ </script>
+```
+
+##API
+
+###Utility
+
+**<a name="difference" href="#difference">.difference(self).from(that)</a>**
+
+Return elements of Array `self` not in Array `that`.
+
+###Value predicates
+
+**<a name="value-in" href="#value-in">.is(self).in(that)</a>**
+
+Return `true` if value `self` exists in an Array `that`.
+
+**<a name="value-all" href="#value-all">.is(self).all(predicate)</a>**
+
+Return `true` if all values in Array `self` satisfy Function `predicate`.
+
+**<a name="value-typeof" href="#value-typeof">.is(self).typeof(type)</a>**
+
+Return `true` if all values in Array `self` satisfy String `type`.
+
+**<a name="value-instanceof" href="#value-instanceof">.is(self).instanceof(predicate)</a>**
+
+Return `true` if all values in Array `self` are instanceof Constructor.
+
+###Object predicates
+
+**<a name="key-in" href="#key-in">.key(property).in(self)</a>**
+
+Return `true` if `key` exists in Object `self`.
+
+**<a name="key-defined" href="#key-defined">.is(self).key(property).defined()</a>**
+
+Return `true` if `key` exists for each Object in Array `self`.
+
+**<a name="key-all" href="#key-all">.is(self).key(property).all(predicate)</a>**
+
+Return `true` if Object[key] satisfies Function `predicate` for each Object in Array `self`.
+
+###jQuery
+
+**<a name="parse" href="#parse">.parse(html)</a>**
+
+Return jQuery Object (Array) from HTML String (Array).
+
+**<a name="stringify" href="#stringify">.stringify(object)</a>**
+
+Return HTML String (Array) from jQuery Object (Array).
+
+**<a name="similar" href="#similar">.similar(self).to(that)</a>**
+
+Return `true` if DOM node `self` and `that` have the same HTML content or are the same ID.
View
78 crackle.js
@@ -10,6 +10,38 @@
// ==/ClosureCompiler==
(function($) {
+ "use strict";
+ if (!Array.prototype.indexOf) {
+ Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
+ if (this == null) {
+ throw new TypeError();
+ }
+ var t = Object(this);
+ var len = t.length >>> 0;
+ if (len === 0) {
+ return -1;
+ }
+ var n = 0;
+ if (arguments.length > 0) {
+ n = Number(arguments[1]);
+ if (n != n) { // shortcut for verifying if it's NaN
+ n = 0;
+ } else if (n != 0 && n != Infinity && n != -Infinity) {
+ n = (n > 0 || -1) * Math.floor(Math.abs(n));
+ }
+ }
+ if (n >= len) {
+ return -1;
+ }
+ var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
+ for (; k < len; k++) {
+ if (k in t && t[k] === searchElement) {
+ return k;
+ }
+ }
+ return -1;
+ }
+ }
var Crackle = {
/**
* Return elements of `self` not in `that`
@@ -33,7 +65,21 @@
};
},
/**
- * Assertions and contracts
+ * Key verifier
+ */
+ key: function(key) {
+ return {
+ in: function(self) {
+ for (var property in self) {
+ if (key === property) {
+ return true;
+ }
+ } return false;
+ }
+ };
+ },
+ /**
+ * Collection verifiers
*/
is: function(self) {
return {
@@ -99,6 +145,36 @@
} else {
return (self instanceof type);
}
+ },
+ key: function(property) {
+ return {
+ /**
+ * Return {Boolean} that a predicate is satisfied
+ * for each Object[property] in an Array
+ * @param self {Array}
+ * @param property {String}
+ * @param predicate {Function}
+ */
+ all: function(predicate) {
+ for (var i = 0; i < self.length; i++) {
+ if (!predicate(self[i][property])) {
+ return false;
+ }
+ } return true;
+ },
+ /**
+ * Return {Boolean} if property exists for each Object in an Array
+ * @param self {Array}
+ * @param property {String}
+ */
+ defined: function() {
+ for (var i = 0; i < self.length; i++) {
+ if (!Crackle.key(property).in(self[i])) {
+ return false;
+ }
+ } return true;
+ }
+ }
}
};
}
View
46 test/crackle.test.js
@@ -18,34 +18,60 @@ vows.describe('Crackle').addBatch({
assert.deepEqual(topic, [1, 6, 7, 8, 9, 10]);
}
},
- 'Element not in list': {
+ 'Key in Object': {
+ topic: Crackle.key('foo').in({foo: 'bar', bar: 'foo'}),
+ 'true': function(topic) {
+ assert.equal(topic, true);
+ }
+ },
+ 'Value not in list': {
topic: !Crackle.is(1).in(2, 3, 4, 5),
'true': function(topic) {
- assert.deepEqual(topic, true);
+ assert.equal(topic, true);
}
},
- 'Element in array': {
+ 'Value in Array': {
topic: Crackle.is(3).in([1, 2, 3, 4, 5]),
'true': function(topic) {
- assert.deepEqual(topic, true);
+ assert.equal(topic, true);
}
},
- 'All elements satisfy predicate': {
+ 'All values satisfy predicate': {
topic: Crackle.is(set).all(function(e) { return e >= 1; }),
'true': function(topic) {
- assert.deepEqual(topic, true);
+ assert.equal(topic, true);
}
},
- 'All elements are typeof "number"': {
+ 'All values are typeof "number"': {
topic: Crackle.is(set).typeof('number'),
'true': function(topic) {
- assert.deepEqual(topic, true);
+ assert.equal(topic, true);
}
},
- 'All elements are instanceof Object': {
+ 'All values are instanceof Object': {
topic: Crackle.is([{}, {}]).instanceof(Object),
'true': function(topic) {
- assert.deepEqual(topic, true);
+ assert.equal(topic, true);
+ }
+ },
+ 'All values of Object[key] in Object Array satisfy predicate': {
+ topic: Crackle.is([
+ {firstname: 'John', lastname: 'Smith'},
+ {firstname: 'Jane', lastname: 'Doe'}
+ ]).key('firstname').all(function(name) {
+ return name[0] === 'J';
+ }),
+ 'true': function(topic) {
+ assert.equal(topic, true);
+ }
+ },
+ 'Key in each Object in Object Array': {
+ topic: Crackle.is([
+ {firstname: 'John', lastname: 'Smith'},
+ {firstname: 'Jane', lastname: 'Doe'}
+ ]).key('firstname').defined(),
+ 'true': function(topic) {
+ assert.equal(topic, true);
}
}
}).export(module);

No commit comments for this range

Something went wrong with that request. Please try again.