You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2013/01/08 00:10:47 UTC

[2/4] js commit: Bring the API for slice() more in line with the W3C spec.

Bring the API for slice() more in line with the W3C spec.


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/190046b1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/190046b1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/190046b1

Branch: refs/heads/master
Commit: 190046b1d999a74dded461659ba4144424642926
Parents: 85cd7b1
Author: Braden Shepherdson <br...@chromium.org>
Authored: Mon Jan 7 15:37:02 2013 -0500
Committer: Braden Shepherdson <br...@chromium.org>
Committed: Mon Jan 7 15:37:02 2013 -0500

----------------------------------------------------------------------
 lib/common/plugin/File.js |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/190046b1/lib/common/plugin/File.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/File.js b/lib/common/plugin/File.js
index 8e59cf1..74b602f 100644
--- a/lib/common/plugin/File.js
+++ b/lib/common/plugin/File.js
@@ -34,32 +34,47 @@ var File = function(name, fullPath, type, lastModifiedDate, size){
     this.type = type || null;
     this.lastModifiedDate = lastModifiedDate || null;
     this.size = size || 0;
+
+    // These store the absolute start and end for slicing the file.
     this.start = 0;
     this.end = size;
 };
 
+/**
+ * Returns a "slice" of the file. Since Cordova Files don't contain the actual
+ * content, this really returns a File with adjusted start and end.
+ * Slices of slices are supported.
+ * start {Number} The index at which to start the slice (inclusive).
+ * end {Number} The index at which to end the slice (exclusive).
+ */
 File.prototype.slice = function(start, end) {
     if (arguments.length == 0) {
-        this.start = 0;
-        this.end = this.size;
-        return;
+        return this;
     }
 
+    var size = this.end - this.start;
+    var newStart = 0;
+    var newEnd = size;
     if (arguments.length >= 1) {
         if (start < 0) {
-            this.start = Math.max(this.size + start, 0);
+            newStart = Math.max(size + start, 0);
         } else {
-            this.start = Math.min(this.size, start);
+            newStart = Math.min(size, start);
         }
     }
 
     if (arguments.length >= 2) {
         if (end < 0) {
-            this.end = Math.max(this.size + end, 0);
+            newEnd = Math.max(size + end, 0);
         } else {
-            this.end = Math.min(end, this.size);
+            newEnd = Math.min(end, size);
         }
     }
+
+    var newFile = new File(this.name, this.fullPath, this.type, this.lastModifiedData, this.size);
+    newFile.start = this.start + newStart;
+    newFile.end = this.start + newEnd;
+    return newFile;
 };