OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Type of a root directory. |
| 7 * @enum |
| 8 */ |
| 9 var RootType = { |
| 10 DOWNLOADS: 'downloads', |
| 11 ARCHIVE: 'archive', |
| 12 REMOVABLE: 'removable', |
| 13 GDATA: 'gdata' |
| 14 }; |
| 15 |
| 16 /** |
| 17 * Top directory for each root type. |
| 18 * @type {Object.<RootType,string>} |
| 19 */ |
| 20 var RootDirectory = { |
| 21 DOWNLOADS: '/Downloads', |
| 22 ARCHIVE: '/archive', |
| 23 REMOVABLE: '/removable', |
| 24 GDATA: '/drive' |
| 25 }; |
| 26 |
| 27 var PathUtil = {}; |
| 28 |
| 29 /** |
| 30 * @param {string} path Path starting with '/'. |
| 31 * @return {string} Root directory (starting with '/'). |
| 32 */ |
| 33 PathUtil.getRootDirectory = function(path) { |
| 34 var i = path.indexOf('/', 1); |
| 35 return i === -1 ? path.substring(0) : path.substring(0, i); |
| 36 }; |
| 37 |
| 38 /** |
| 39 * @param {string} path Any unix-style path (may start or not start from root). |
| 40 * @return {Array.<string>} path components |
| 41 */ |
| 42 PathUtil.split = function(path) { |
| 43 var fromRoot = false; |
| 44 if (path[0] === '/') { |
| 45 fromRoot = true; |
| 46 path = path.substring(1); |
| 47 } |
| 48 |
| 49 var components = path.split('/'); |
| 50 if (fromRoot) |
| 51 components[0] = '/' + components[0]; |
| 52 return components; |
| 53 }; |
| 54 |
| 55 /** |
| 56 * Join path components into a single path. Can be called either with a list of |
| 57 * components as arguments, or with an array of components as the only argument. |
| 58 * |
| 59 * Examples: |
| 60 * Path.join('abc', 'def') -> 'abc/def' |
| 61 * Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi' |
| 62 * Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi' |
| 63 * |
| 64 * @return {string} Resulting path. |
| 65 */ |
| 66 PathUtil.join = function() { |
| 67 var components; |
| 68 |
| 69 if (arguments.length === 1 && typeof(arguments[0]) === 'object') { |
| 70 components = arguments[0]; |
| 71 } else { |
| 72 components = arguments; |
| 73 } |
| 74 |
| 75 var path = ''; |
| 76 for (var i = 0; i < components.length; i++) { |
| 77 if (components[i][0] === '/') { |
| 78 path = components[i]; |
| 79 continue; |
| 80 } |
| 81 if (path.length === 0 || path[path.length - 1] !== '/') |
| 82 path += '/'; |
| 83 path += components[i]; |
| 84 } |
| 85 return path; |
| 86 }; |
| 87 |
| 88 /** |
| 89 * @param {string} path Path starting with '/'. |
| 90 * @return {RootType} RootType.DOWNLOADS, RootType.GDATA etc. |
| 91 */ |
| 92 PathUtil.getRootType = function(path) { |
| 93 var rootDir = PathUtil.getRootDirectory(path); |
| 94 for (var type in RootDirectory) { |
| 95 if (rootDir === RootDirectory[type]) |
| 96 return RootType[type]; |
| 97 } |
| 98 }; |
| 99 |
| 100 /** |
| 101 * @param {string} path Any path. |
| 102 * @return {string} The root path. |
| 103 */ |
| 104 PathUtil.getRootPath = function(path) { |
| 105 var type = PathUtil.getRootType(path); |
| 106 |
| 107 if (type == RootType.DOWNLOADS || type == RootType.GDATA) |
| 108 return PathUtil.getRootDirectory(path); |
| 109 |
| 110 if (type == RootType.ARCHIVE || type == RootType.REMOVABLE) { |
| 111 var components = PathUtil.split(path); |
| 112 if (components.length > 1) { |
| 113 return PathUtil.join(components[0], components[1]); |
| 114 } else { |
| 115 return components[0]; |
| 116 } |
| 117 } |
| 118 |
| 119 return '/'; |
| 120 }; |
| 121 |
| 122 /** |
| 123 * @param {string} path A path. |
| 124 * @return {boolean} True if it is a path to the root. |
| 125 */ |
| 126 PathUtil.isRootPath = function(path) { |
| 127 return PathUtil.getRootPath(path) === path; |
| 128 }; |
| 129 |
OLD | NEW |