react-native-file-access
Version:
Filesystem access for React Native
84 lines (80 loc) • 1.9 kB
JavaScript
;
/**
* Escape for use as literal string in a regex.
*/
function regexEscape(literal) {
return literal.replace(/[\^$\\.*+?()[\]{}|]/g, '\\$&');
}
/**
* Condense consecutive separators.
*/
function normalizeSeparator(path, separator) {
const sepRe = new RegExp(`(${regexEscape(separator)}){2,}`, 'g');
return path.replace(sepRe, separator.replace(/\$/g, '$$$$'));
}
/**
* Split path on last separator.
*/
function splitPath(path, separator) {
let norm = normalizeSeparator(path, separator);
if (norm === separator) {
return {
dir: separator,
base: ''
};
}
if (norm.endsWith(separator)) {
norm = norm.substring(0, norm.length - separator.length);
}
const idx = norm.lastIndexOf(separator);
if (idx === -1) {
return {
dir: '.',
base: norm
};
}
return {
dir: norm.substring(0, idx),
base: norm.substring(idx + separator.length)
};
}
/**
* Utility functions for working with Android scoped storage.
*/
export const AndroidScoped = {
/**
* Append a path segment to an Android scoped storage content uri.
*/
appendPath(basePath, segment) {
return basePath + encodeURIComponent('/' + segment);
}
};
export const Util = {
/**
* Get the file/folder name from the end of the path.
*/
basename(path, separator = '/') {
return splitPath(path, separator).base;
},
/**
* Get the path containing the file/folder.
*/
dirname(path, separator = '/') {
return splitPath(path, separator).dir;
},
/**
* Get the file extension.
*/
extname(path, separator = '/') {
const extIdx = path.lastIndexOf('.');
if (extIdx <= 0) {
return '';
}
const sepIdx = path.lastIndexOf(separator);
if (sepIdx === -1 || extIdx > sepIdx + separator.length) {
return path.substring(extIdx + 1);
}
return '';
}
};
//# sourceMappingURL=util.js.map