Added path methods to the FileSystem methods. Added unit tests.

This commit is contained in:
atanasovg
2014-05-07 14:55:43 +03:00
parent e84f305b6d
commit beba0ae4f4
5 changed files with 124 additions and 10 deletions

View File

@@ -127,4 +127,24 @@ export declare module knownFolders {
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
*/
export function temp(): Folder;
}
/**
* Enables path-specific operations like join, extension, etc.
*/
export declare module path {
/**
* Normalizes a path, taking care of occurrances like ".." and "//"
*/
export function normalize(path: string): string;
/**
* Joins all the provided string components, forming a valid and normalized path.
*/
export function join(...paths: string[]): string;
/**
* Gets the string used to separate file paths.
*/
export var separator: string;
}

View File

@@ -112,7 +112,7 @@ export class FileSystemEntity {
var fileAccess = getFileAccess();
var path = parentFolder.path;
var newPath = fileAccess.concatPath(path, newName);
var newPath = fileAccess.joinPath(path, newName);
var that = this;
var localSucceess = function () {
@@ -294,7 +294,7 @@ export class Folder extends FileSystemEntity {
*/
public contains(name: string): boolean {
var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name);
var path = fileAccess.joinPath(this.path, name);
if (fileAccess.fileExists(path)) {
return true;
@@ -333,7 +333,7 @@ export class Folder extends FileSystemEntity {
*/
public getFile(name: string): File {
var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name);
var path = fileAccess.joinPath(this.path, name);
var onError = function (error) {
throw error;
@@ -352,7 +352,7 @@ export class Folder extends FileSystemEntity {
*/
public getFolder(name: string): Folder {
var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name);
var path = fileAccess.joinPath(this.path, name);
var onError = function (error) {
throw error;
@@ -461,4 +461,29 @@ export module knownFolders {
return _temp;
}
}
/**
* Enables path-specific operations like join, extension, etc.
*/
export module path {
/**
* Normalizes a path, taking care of occurrances like ".." and "//"
*/
export function normalize(path: string): string {
return getFileAccess().normalizePath(path);
}
/**
* Joins all the provided string components, forming a valid and normalized path.
*/
export function join(...paths: string[]): string {
var fileAccess = getFileAccess();
return fileAccess.joinPaths(paths);
}
/**
* Gets the string used to separate file paths.
*/
export var separator = getFileAccess().getPathSeparator();
}

View File

@@ -2,7 +2,7 @@
import textModule = require("text/text");
export class FileSystemAccess {
private _pathSeparator = java.io.File.separator;
private _pathSeparator = java.io.File.separator.toString();
public getLastModified(path: string): Date {
var javaFile = new java.io.File(path);
@@ -90,10 +90,6 @@ export class FileSystemAccess {
return exists && dir;
}
public concatPath(left: string, right: string): string {
return left + this._pathSeparator + right;
}
public deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
try {
var javaFile = new java.io.File(path);
@@ -398,4 +394,38 @@ export class FileSystemAccess {
}
}
}
public getPathSeparator(): string {
return this._pathSeparator;
}
public normalizePath(path: string): string {
var file = new java.io.File(path);
return file.getAbsolutePath();
}
public joinPath(left: string, right: string): string {
var file1 = new java.io.File(left);
var file2 = new java.io.File(file1, right);
return file2.getAbsolutePath();
}
public joinPaths(paths: string[]): string {
if (!paths || paths.length === 0) {
return "";
}
if (paths.length === 1) {
return paths[0];
}
var i,
result = paths[0];
for (i = 1; i < paths.length; i++) {
result = this.joinPath(result, paths[i]);
}
return this.normalizePath(result);
}
}

View File

@@ -13,7 +13,7 @@ export declare class FileSystemAccess {
fileExists(path: string): boolean;
folderExists(path: string): boolean;
concatPath(left: string, right: string): string;
deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any);
deleteFolder(path: string, isKnown: boolean, onSuccess?: () => any, onError?: (error: any) => any);
emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any): void;
@@ -25,5 +25,11 @@ export declare class FileSystemAccess {
writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any, encoding?: string);
getFileExtension(path: string): string;
// path methods
getPathSeparator(): string;
normalizePath(path: string): string;
joinPath(left: string, right: string): string;
joinPaths(paths: string[]): string;
}

View File

@@ -346,4 +346,37 @@ export class FileSystemAccess {
}
}
}
public getPathSeparator(): string {
return "/";
}
public normalizePath(path: string): string {
var nsString: Foundation.NSString = Foundation.NSString.stringWithString(path);
var normalized = nsString.stringByStandardizingPath();
return normalized;
}
public joinPath(left: string, right: string): string {
var nsString: Foundation.NSString = Foundation.NSString.stringWithString(left);
return nsString.stringByAppendingPathComponent(right);
}
public joinPaths(paths: string[]): string {
if (!paths || paths.length === 0) {
return "";
}
var nsArray = new Foundation.NSMutableArray(paths.length);
var i;
for (i = 0; i < paths.length; i++) {
nsArray.addObject(paths[i]);
}
// TODO: Static methods return NSString instance to enable its methods
var nsString: any = Foundation.NSString.pathWithComponents(nsArray);
return nsString.stringByStandardizingPath();
}
}