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 {
|
2018-04-16 15:21:24 +00:00
|
|
|
return this._parts.length ? this._parts[this._parts.length - 1] : null;
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isDocument(): boolean {
|
2018-04-16 15:21:24 +00:00
|
|
|
return this._parts.length % 2 === 0;
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-04-21 23:25:36 +00:00
|
|
|
return this._parts.length > 1
|
2018-03-20 16:07:37 +00:00
|
|
|
? new Path(this._parts.slice(0, this._parts.length - 1))
|
|
|
|
: null;
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package
|
|
|
|
*/
|
2018-01-16 17:08:44 +00:00
|
|
|
static fromName(name: string): Path {
|
2018-04-16 15:21:24 +00:00
|
|
|
if (!name) return new Path([]);
|
2017-09-26 13:57:25 +00:00
|
|
|
const parts = name.split('/');
|
2018-04-16 15:21:24 +00:00
|
|
|
return new Path(parts);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
}
|