monaco-editor
Version:
A browser based code editor
145 lines (144 loc) • 5.5 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as paths from './paths.js';
import { URI } from './uri.js';
import { equalsIgnoreCase } from './strings.js';
import { Schemas } from './network.js';
import { isLinux, isWindows } from './platform.js';
export function hasToIgnoreCase(resource) {
// A file scheme resource is in the same platform as code, so ignore case for non linux platforms
// Resource can be from another platform. Lowering the case as an hack. Should come from File system provider
return resource && resource.scheme === Schemas.file ? !isLinux : true;
}
export function basenameOrAuthority(resource) {
return basename(resource) || resource.authority;
}
export function isEqual(first, second, ignoreCase) {
if (ignoreCase === void 0) { ignoreCase = hasToIgnoreCase(first); }
var identityEquals = (first === second);
if (identityEquals) {
return true;
}
if (!first || !second) {
return false;
}
if (ignoreCase) {
return equalsIgnoreCase(first.toString(), second.toString());
}
return first.toString() === second.toString();
}
export function basename(resource) {
return paths.basename(resource.path);
}
/**
* Return a URI representing the directory of a URI path.
*
* @param resource The input URI.
* @returns The URI representing the directory of the input URI.
*/
export function dirname(resource) {
if (resource.scheme === Schemas.file) {
return URI.file(paths.dirname(fsPath(resource)));
}
var dirname = paths.dirname(resource.path, '/');
if (resource.authority && dirname.length && dirname.charCodeAt(0) !== 47 /* Slash */) {
return null; // If a URI contains an authority component, then the path component must either be empty or begin with a CharCode.Slash ("/") character
}
return resource.with({
path: dirname
});
}
/**
* Join a URI path with a path fragment and normalizes the resulting path.
*
* @param resource The input URI.
* @param pathFragment The path fragment to add to the URI path.
* @returns The resulting URI.
*/
export function joinPath(resource, pathFragment) {
var joinedPath;
if (resource.scheme === Schemas.file) {
joinedPath = URI.file(paths.join(fsPath(resource), pathFragment)).path;
}
else {
joinedPath = paths.join(resource.path, pathFragment);
}
return resource.with({
path: joinedPath
});
}
/**
* Normalizes the path part of a URI: Resolves `.` and `..` elements with directory names.
*
* @param resource The URI to normalize the path.
* @returns The URI with the normalized path.
*/
export function normalizePath(resource) {
var normalizedPath;
if (resource.scheme === Schemas.file) {
normalizedPath = URI.file(paths.normalize(fsPath(resource))).path;
}
else {
normalizedPath = paths.normalize(resource.path);
}
return resource.with({
path: normalizedPath
});
}
/**
* Returns the fsPath of an URI where the drive letter is not normalized.
* See #56403.
*/
export function fsPath(uri) {
var value;
if (uri.authority && uri.path.length > 1 && uri.scheme === 'file') {
// unc path: file://shares/c$/far/boo
value = "//" + uri.authority + uri.path;
}
else if (isWindows
&& uri.path.charCodeAt(0) === 47 /* Slash */
&& (uri.path.charCodeAt(1) >= 65 /* A */ && uri.path.charCodeAt(1) <= 90 /* Z */ || uri.path.charCodeAt(1) >= 97 /* a */ && uri.path.charCodeAt(1) <= 122 /* z */)
&& uri.path.charCodeAt(2) === 58 /* Colon */) {
value = uri.path.substr(1);
}
else {
// other path
value = uri.path;
}
if (isWindows) {
value = value.replace(/\//g, '\\');
}
return value;
}
/**
* Data URI related helpers.
*/
export var DataUri;
(function (DataUri) {
DataUri.META_DATA_LABEL = 'label';
DataUri.META_DATA_DESCRIPTION = 'description';
DataUri.META_DATA_SIZE = 'size';
DataUri.META_DATA_MIME = 'mime';
function parseMetaData(dataUri) {
var metadata = new Map();
// Given a URI of: data:image/png;size:2313;label:SomeLabel;description:SomeDescription;base64,77+9UE5...
// the metadata is: size:2313;label:SomeLabel;description:SomeDescription
var meta = dataUri.path.substring(dataUri.path.indexOf(';') + 1, dataUri.path.lastIndexOf(';'));
meta.split(';').forEach(function (property) {
var _a = property.split(':'), key = _a[0], value = _a[1];
if (key && value) {
metadata.set(key, value);
}
});
// Given a URI of: data:image/png;size:2313;label:SomeLabel;description:SomeDescription;base64,77+9UE5...
// the mime is: image/png
var mime = dataUri.path.substring(0, dataUri.path.indexOf(';'));
if (mime) {
metadata.set(DataUri.META_DATA_MIME, mime);
}
return metadata;
}
DataUri.parseMetaData = parseMetaData;
})(DataUri || (DataUri = {}));