cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [2/4] js commit: Bring the API for slice() more in line with the W3C spec.
Date Mon, 07 Jan 2013 23:10:47 GMT
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 <braden@chromium.org>
Authored: Mon Jan 7 15:37:02 2013 -0500
Committer: Braden Shepherdson <braden@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;
 };
 
 


Mime
View raw message