[css] requests update

This commit is contained in:
Martin Aeschlimann 2020-06-08 13:40:26 +02:00
parent cbb1610c12
commit b49d4cf5e6
2 changed files with 27 additions and 2 deletions

View File

@ -113,9 +113,26 @@ export function basename(uri: string) {
return uri.substr(lastIndexOfSlash + 1);
}
const Slash = '/'.charCodeAt(0);
const Dot = '.'.charCodeAt(0);
export function extname(uri: string) {
for (let i = uri.length - 1; i >= 0; i--) {
const ch = uri.charCodeAt(i);
if (ch === Dot) {
if (i > 0 && uri.charCodeAt(i - 1) !== Slash) {
return uri.substr(i);
} else {
break;
}
} else if (ch === Slash) {
break;
}
}
return '';
}
export function isAbsolutePath(path: string) {
return path.charCodeAt(0) === Slash;
}

View File

@ -4,8 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as assert from 'assert';
import { joinPath, normalizePath, resolvePath } from '../requests';
import { joinPath, normalizePath, resolvePath, extname } from '../requests';
suite('requests', () => {
test('join', async function () {
@ -51,4 +50,13 @@ suite('requests', () => {
assertNormalize('..', '');
assertNormalize('/..', '/');
});
test('extname', async function () {
function assertExtName(input: string, expected: string) {
assert.equal(extname(input), expected, input);
}
assertExtName('foo://a/foo/bar', '');
assertExtName('foo://a/foo/bar.foo', '.foo');
assertExtName('foo://a/foo/.foo', '');
});
});