diff options
Diffstat (limited to 'node_modules/get-source/impl')
| -rw-r--r-- | node_modules/get-source/impl/SyncPromise.js | 51 | ||||
| -rw-r--r-- | node_modules/get-source/impl/path.js | 62 |
2 files changed, 113 insertions, 0 deletions
diff --git a/node_modules/get-source/impl/SyncPromise.js b/node_modules/get-source/impl/SyncPromise.js new file mode 100644 index 0000000..1412532 --- /dev/null +++ b/node_modules/get-source/impl/SyncPromise.js @@ -0,0 +1,51 @@ +"use strict"; + +/* ------------------------------------------------------------------------ */ + +module.exports = class SyncPromise { + + constructor (fn) { + try { + fn ( + x => { this.setValue (x, false) }, // resolve + x => { this.setValue (x, true) } // reject + ) + } catch (e) { + this.setValue (e, true) + } + } + + setValue (x, rejected) { + this.val = (x instanceof SyncPromise) ? x.val : x + this.rejected = rejected || ((x instanceof SyncPromise) ? x.rejected : false) + } + + static valueFrom (x) { + if (x instanceof SyncPromise) { + if (x.rejected) throw x.val + else return x.val + } else { + return x + } + } + + then (fn) { + try { if (!this.rejected) return SyncPromise.resolve (fn (this.val)) } + catch (e) { return SyncPromise.reject (e) } + return this + } + + catch (fn) { + try { if (this.rejected) return SyncPromise.resolve (fn (this.val)) } + catch (e) { return SyncPromise.reject (e) } + return this + } + + static resolve (x) { + return new SyncPromise (resolve => { resolve (x) }) + } + + static reject (x) { + return new SyncPromise ((_, reject) => { reject (x) }) + } +}
\ No newline at end of file diff --git a/node_modules/get-source/impl/path.js b/node_modules/get-source/impl/path.js new file mode 100644 index 0000000..4a0eb3d --- /dev/null +++ b/node_modules/get-source/impl/path.js @@ -0,0 +1,62 @@ +"use strict"; + +/* ------------------------------------------------------------------------ */ + +const isBrowser = (typeof window !== 'undefined') && (window.window === window) && window.navigator +const cwd = isBrowser ? window.location.href : process.cwd () + +const urlRegexp = new RegExp ("^((https|http)://)?[a-z0-9A-Z]{3}\.[a-z0-9A-Z][a-z0-9A-Z]{0,61}?[a-z0-9A-Z]\.com|net|cn|cc (:s[0-9]{1-4})?/$") + +/* ------------------------------------------------------------------------ */ + +const path = module.exports = { + + concat (a, b) { + + const a_endsWithSlash = (a[a.length - 1] === '/'), + b_startsWithSlash = (b[0] === '/') + + return a + ((a_endsWithSlash || b_startsWithSlash) ? '' : '/') + + ((a_endsWithSlash && b_startsWithSlash) ? b.substring (1) : b) + }, + + resolve (x) { + + if (path.isAbsolute (x)) { + return path.normalize (x) } + + return path.normalize (path.concat (cwd, x)) + }, + + normalize (x) { + + let output = [], + skip = 0 + + x.split ('/').reverse ().filter (x => x !== '.').forEach (x => { + + if (x === '..') { skip++ } + else if (skip === 0) { output.push (x) } + else { skip-- } + }) + + const result = output.reverse ().join ('/') + + return ((isBrowser && (result[0] === '/')) ? result[1] === '/' ? window.location.protocol : window.location.origin : '') + result + }, + + isData: x => x.indexOf ('data:') === 0, + + isURL: x => urlRegexp.test (x), + + isAbsolute: x => (x[0] === '/') || /^[^\/]*:/.test (x), + + relativeToFile (a, b) { + + return (path.isData (a) || path.isAbsolute (b)) ? + path.normalize (b) : + path.normalize (path.concat (a.split ('/').slice (0, -1).join ('/'), b)) + } +} + +/* ------------------------------------------------------------------------ */ |
