2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Path representation wrapper
|
|
|
|
*/
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
/**
|
2017-09-26 13:57:25 +00:00
|
|
|
* @class Path
|
|
|
|
*/
|
|
|
|
export default class Path {
|
|
|
|
_parts: string[];
|
|
|
|
|
|
|
|
constructor(pathComponents: string[]) {
|
|
|
|
this._parts = pathComponents;
|
|
|
|
}
|
|
|
|
|
|
|
|
get id(): string | null {
|
|
|
|
if (this._parts.length > 0) {
|
|
|
|
return this._parts[this._parts.length - 1];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDocument(): boolean {
|
|
|
|
return this._parts.length > 0 && this._parts.length % 2 === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isCollection(): boolean {
|
|
|
|
return this._parts.length % 2 === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get relativeName(): string {
|
|
|
|
return this._parts.join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
child(relativePath: string): Path {
|
|
|
|
return new Path(this._parts.concat(relativePath.split('/')));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent(): Path | null {
|
|
|
|
if (this._parts.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Path(this._parts.slice(0, this._parts.length - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package
|
|
|
|
*/
|
2018-01-16 17:08:44 +00:00
|
|
|
static fromName(name: string): Path {
|
2017-09-26 13:57:25 +00:00
|
|
|
const parts = name.split('/');
|
|
|
|
|
|
|
|
if (parts.length === 0) {
|
|
|
|
return new Path([]);
|
|
|
|
}
|
|
|
|
return new Path(parts);
|
|
|
|
}
|
|
|
|
}
|