Permalink
Browse files

Release v0.1.3

  • Loading branch information...
Baggz committed Mar 19, 2012
1 parent 8b3f2e1 commit be5338402277c4f6f738b9aae70ad17f5d365821
Showing with 138 additions and 40 deletions.
  1. +2 −2 README.md
  2. +66 −28 dist/latest.js
  3. +4 −4 dist/latest.min.js
  4. +1 −1 package.json
  5. +47 −5 src/pinch.js
  6. +18 −0 tests/tests.js
View
@@ -75,8 +75,8 @@ Releases are available for download from GitHub.
| **Version** | **Description** | **Size** | **Action** |
|:------------|:----------------|:---------|:-----------|
-| `pinch.js` | *uncompressed, with comments* | 4.71 KB (1.51 KB gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.js) |
-| `pinch.min.js` | *compressed, without comments* | 1.67 KB (761 bytes gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.min.js) |
+| `pinch.js` | *uncompressed, with comments* | 5.34 KB (1.68 KB gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.js) |
+| `pinch.min.js` | *compressed, without comments* | 1.85 KB (852 bytes gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.min.js) |
<a name="Usage"></a>
## Usage [&uarr;](#Contents)
View
@@ -18,10 +18,28 @@
return Object.prototype.toString.call(input) === '[object Array]';
};
+ /**
+ * IsObject
+ *
+ * @param {object} input
+ */
var isObject = function(input) {
return Object.prototype.toString.call(input) === '[object Object]';
};
+ /**
+ * IsEqualArray
+ *
+ * @param {object} arr1
+ * @param {object} arr2
+ */
+ var isEqualArray = function(arr1, arr2) {
+ if (arr1.length !== arr2.length) return false;
+ return arr1.every(function(value, index, context) {
+ return arr2[index] === value;
+ });
+ };
+
/**
* Each
*
@@ -45,35 +63,49 @@
};
/**
- * Clone
+ * ParseNotation
*
- * @param {object} input
+ * @param {string} notation
*/
- var clone = function(input) {
-
- var output;
-
- if (isArray(input)) {
- output = [];
- each(input, function(index, value) {
- output.push(clone(value));
- });
- return output;
- }
+ var parseNotation = function(notation) {
+
+ var chunks = [];
+ var openBracket = false;
+ var i = 0;
+ var len = notation.length;
+ var tempChunk = '';
+
+ var addChunk = function() {
+ if (tempChunk) {
+ chunks.push(tempChunk);
+ tempChunk = '';
+ }
+ };
- if (isObject(input)) {
- output = {};
- each(input, function(key, value) {
- output[key] = clone(value);
- });
- return output;
+ for (; i < len; i++) {
+ if (notation[i].match(/\[|\]/)) {
+ addChunk();
+ if (notation[i] === ']') {
+ openBracket = false;
+ } else {
+ openBracket = true;
+ }
+ } else if (notation[i] !== '"' && notation[i] !== '\'') {
+ if (notation[i] === '.' && !openBracket) {
+ addChunk();
+ } else {
+ tempChunk += notation[i];
+ }
+ }
+ if (i === len - 1) {
+ addChunk();
+ }
}
- return input;
+ return chunks;
};
-
/**
* Pinch
*
@@ -100,10 +132,10 @@
this.instance = JSON.parse(instance);
this.json = true;
} else {
- this.instance = clone(instance);
+ this.instance = instance;
}
- this.pattern = pattern;
+ this.pattern = (typeof pattern === 'string') ? pattern.replace(/'/g, '"') : pattern;
this.replacement = replacement;
// Creates an index for the passed instance
@@ -123,7 +155,6 @@
this.index = this.index || [];
-
path = path || '';
each(instance, function(key, value) {
@@ -143,7 +174,7 @@
} else {
currentPath = path + '["' + key + '"]';
}
-
+ console.log(currentPath);
self.index.push(currentPath);
if (typeof value === 'object') {
@@ -170,8 +201,15 @@
}
// If the pattern is a string and matches the key
- if (typeof self.pattern === 'string' && value === self.pattern) {
- return self.replaceValue(value);
+ if (typeof self.pattern === 'string') {
+
+ var valueTree = parseNotation(value);
+ var patternTree = parseNotation(self.pattern);
+
+ if (isEqualArray(valueTree, patternTree)) {
+ return self.replaceValue(value);
+ }
+
}
});
@@ -190,7 +228,7 @@
var self = this;
- var tree = path.match(/([a-zA-Z0-9\s]+)/g);
+ var tree = parseNotation(path);
tree.reduce(function(previousValue, currentValue, index) {
if (index === tree.length - 1) {
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -2,7 +2,7 @@
"name": "pinch",
"description": "String.replace but for JavaScript objects (and JSON).",
- "version": "0.1.2",
+ "version": "0.1.3",
"author": "František Hába <hello@frantisekhaba.com>",
"devDependencies": {
View
@@ -62,7 +62,49 @@
}
};
- var treeRe = /([a-zA-Z0-9\s]+)/g;
+ /**
+ * ParseNotation
+ *
+ * @param {string} notation
+ */
+ var parseNotation = function(notation) {
+
+ var chunks = [];
+ var openBracket = false;
+ var i = 0;
+ var len = notation.length;
+ var tempChunk = '';
+
+ var addChunk = function() {
+ if (tempChunk) {
+ chunks.push(tempChunk);
+ tempChunk = '';
+ }
+ };
+
+ for (; i < len; i++) {
+ if (notation[i].match(/\[|\]/)) {
+ addChunk();
+ if (notation[i] === ']') {
+ openBracket = false;
+ } else {
+ openBracket = true;
+ }
+ } else if (notation[i] !== '"' && notation[i] !== '\'') {
+ if (notation[i] === '.' && !openBracket) {
+ addChunk();
+ } else {
+ tempChunk += notation[i];
+ }
+ }
+ if (i === len - 1) {
+ addChunk();
+ }
+ }
+
+ return chunks;
+
+ };
/**
* Pinch
@@ -132,7 +174,7 @@
} else {
currentPath = path + '["' + key + '"]';
}
-
+ console.log(currentPath);
self.index.push(currentPath);
if (typeof value === 'object') {
@@ -161,8 +203,8 @@
// If the pattern is a string and matches the key
if (typeof self.pattern === 'string') {
- var valueTree = value.match(treeRe),
- patternTree = self.pattern.match(treeRe);
+ var valueTree = parseNotation(value);
+ var patternTree = parseNotation(self.pattern);
if (isEqualArray(valueTree, patternTree)) {
return self.replaceValue(value);
@@ -186,7 +228,7 @@
var self = this;
- var tree = path.match(treeRe);
+ var tree = parseNotation(path);
tree.reduce(function(previousValue, currentValue, index) {
if (index === tree.length - 1) {
View
@@ -1105,4 +1105,22 @@ suite('More Ambitious Notations', function() {
expect(result.user.profile.name.firstName["my First name"].name).to.be.eql('Test');
});
+});
+
+/**
+ * Even More ambitious notations
+ * --------------------
+ */
+suite('Even More Ambitious Notations', function() {
+
+ var testObjectDefault = {
+ 'user.name': 'František'
+ };
+
+ test('using the ‘["user.name"]’ notation', function() {
+ var testObject = clone(testObjectDefault);
+ var result = pinch(testObject, '["user.name"]', 'Test');
+ expect(testObject['user.name']).to.be.eql('Test');
+ });
+
});

0 comments on commit be53384

Please sign in to comment.