From a08dbc3d47a4dc8a0748918eada94cddc877d796 Mon Sep 17 00:00:00 2001 From: cgcostume Date: Fri, 8 Mar 2019 11:18:43 +0100 Subject: [PATCH] #110 remove use of nodejs path, tweak example --- examples/labelanchor-example.ts | 4 ++-- source/auxiliaries.ts | 27 +++++++++++++++++++++++++++ source/text/fontfaceloader.ts | 8 ++------ test/auxiliaries.test.ts | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/examples/labelanchor-example.ts b/examples/labelanchor-example.ts index 6bfb8079..b5bc5754 100644 --- a/examples/labelanchor-example.ts +++ b/examples/labelanchor-example.ts @@ -230,8 +230,8 @@ export class LabelAnchorExample extends Example { this._canvas.renderer = this._renderer; // Create some horizontal rules/lines as reference for the different anchors. - const hrStyle = 'z-index: 1; position: absolute; width: 99%; margin: 0;' - + 'border-color: #27aae1; border-style: dashed; border-width: 1px;'; + const hrStyle = 'z-index: 1; position: absolute; width: 98%; margin: 0; margin-left: 1%;' + + 'border: none; border-top: 1pt solid #1c75bc;'; const hr0 = document.createElement('hr'); hr0.setAttribute('style', `${hrStyle} top: 8.3333%;`); diff --git a/source/auxiliaries.ts b/source/auxiliaries.ts index b0f1c1dc..2fa68ce6 100644 --- a/source/auxiliaries.ts +++ b/source/auxiliaries.ts @@ -295,6 +295,33 @@ namespace auxiliaries { return match[1]; } + /** + * Path separator used for path related functions such as dirname and basename. + */ + export const PATH_SEPARATOR = '/'; + + /** + * Returns the directory name of a given (file) path. If no path separator is found, an empty dir name is returned. + * @param path - Path string the directory name should be returned of. + */ + export function dirname(path: string): string { + if (path.includes(PATH_SEPARATOR) === false) { + return ''; + } + return path.substr(0, path.lastIndexOf(PATH_SEPARATOR)).trimLeft(); + } + + /** + * Returns the base name of a given file path. If no path separator is found, the input path is returned. + * @param path - Path string the file/base name should be returned of. + */ + export function basename(path: string): string { + if (path.includes(PATH_SEPARATOR) === false) { + return path; + } + return path.substr(path.lastIndexOf(PATH_SEPARATOR) + 1).trimRight(); + } + } export = auxiliaries; diff --git a/source/text/fontfaceloader.ts b/source/text/fontfaceloader.ts index d0a8e412..560e4a5a 100644 --- a/source/text/fontfaceloader.ts +++ b/source/text/fontfaceloader.ts @@ -1,7 +1,7 @@ /* spellchecker: disable */ -import { log, logIf, LogLevel } from '../auxiliaries'; +import { dirname, log, logIf, LogLevel } from '../auxiliaries'; import { GLfloat2, GLfloat4 } from '../tuples'; import { FontFace } from './fontface'; @@ -10,10 +10,6 @@ import { Glyph } from './glyph'; /* spellchecker: enable */ -/** @todo replace path */ -import Path = require('path'); - - type StringPairs = Map; /** @@ -102,7 +98,7 @@ export class FontFaceLoader { return undefined; } - const path = Path.dirname(url); + const path = dirname(url); let page = pairs.get('file')!; page = page.replace(/['"]+/g, ''); /* remove quotes */ diff --git a/test/auxiliaries.test.ts b/test/auxiliaries.test.ts index 18ea9003..c313161b 100644 --- a/test/auxiliaries.test.ts +++ b/test/auxiliaries.test.ts @@ -239,3 +239,24 @@ describe('auxiliaries GETparameter', () => { // }); }); + + +describe('auxiliaries path', () => { + + it('should return the directory name of a file path', () => { + expect(aux.dirname('https://localhost/file.ext')).to.equal('https://localhost'); + expect(aux.dirname('file.ext')).to.equal(''); + + expect(aux.dirname('localhost/file')).to.equal('localhost'); + expect(aux.dirname('localhost/dir/')).to.equal('localhost/dir'); + }); + + it('should return the base name of a file path', () => { + expect(aux.basename('https://localhost/file.ext')).to.equal('file.ext'); + expect(aux.basename('file.ext')).to.equal('file.ext'); + + expect(aux.basename('localhost/file')).to.equal('file'); + expect(aux.basename('localhost/dir/')).to.equal(''); + }); + +});