-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transpile with babel for proper testing
- Loading branch information
Patrick Kubiak
committed
Oct 10, 2019
1 parent
345daae
commit 3d25cc5
Showing
11 changed files
with
4,486 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
coverage | ||
node_modules | ||
test/test-all.js | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,73 @@ | ||
const path = require('path') | ||
const { src, dest, series } = require('gulp') | ||
const include = require('gulp-include') | ||
const rename = require('gulp-rename') | ||
const rollup = require('gulp-rollup') | ||
const babel = require('gulp-babel') | ||
const header = require('gulp-header') | ||
const Server = require('karma').Server | ||
|
||
const headerTxt = | ||
`/** | ||
* | ||
* Partial local storage polyfill | ||
* | ||
* @author Patrick Kubiak <[email protected]> | ||
* | ||
* Why? | ||
* - For local files, IE and Edge do not have support for local storage | ||
* - local storage can be disabled | ||
* | ||
* Implementation details: | ||
* - Use browser's native implementation if supported | ||
* - Mirror native functionality with an object | ||
* | ||
* Implementation flaws: | ||
* - Not persistent | ||
* - Need to use _localStorage instead of localStorage | ||
* | ||
* Use _localStorage or window._localStorage instead of localStorage or window.localStorage | ||
* because the browser prevents using localStorage or window.localStorage if access is denied for this document | ||
* | ||
*/ | ||
` | ||
|
||
/** | ||
* Babel transpile for dist | ||
*/ | ||
function transpile () { | ||
return src('./src/**/*.js') | ||
.pipe(rollup({ | ||
input: './src/local-storage-poorlyfill.js', | ||
output: { | ||
format: 'iife' | ||
}, | ||
plugins: [ | ||
babel({ | ||
presets: ['@babel/env'] | ||
}) | ||
] | ||
})) | ||
.pipe(header(headerTxt)) | ||
.pipe(dest('./dist/')) | ||
} | ||
|
||
/** | ||
* Combine polyfill and tests into ./test/test-all.js | ||
* Babel transpile for tests | ||
*/ | ||
function combine () { | ||
return src('./test/test.js') | ||
.pipe(include()) | ||
.on('error', console.log) | ||
.pipe(rename({ | ||
suffix: '-all' | ||
function transpileTests () { | ||
return src(['./src/**/*.js', './test/**/*.js']) | ||
.pipe(rollup({ | ||
input: './test/test.js', | ||
output: { | ||
format: 'iife' | ||
}, | ||
plugins: [ | ||
babel({ | ||
presets: ['@babel/env'] | ||
}) | ||
] | ||
})) | ||
.pipe(dest('./test/')) | ||
.pipe(dest('./dist/')) | ||
} | ||
|
||
/** | ||
|
@@ -27,9 +80,14 @@ function test (done) { | |
}, done()).start() | ||
} | ||
|
||
const runTests = series(combine, test) | ||
/** | ||
* Transpile and run tests | ||
*/ | ||
const runTests = series(transpileTests, test) | ||
|
||
exports.combine = combine | ||
// export | ||
exports.transpile = transpile | ||
exports.transpileTests = transpileTests | ||
exports.test = test | ||
exports.runTests = runTests | ||
exports.default = runTests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { _localStorage, _isSupported } from './local-storage.js' | ||
|
||
if (_isSupported()) { | ||
// use browser's implementation | ||
// cannot overwrite window.localStorage | ||
window._localStorage = window.localStorage | ||
} else { | ||
// mirror functionality with an object | ||
// cannot overwrite window.localStorage | ||
window._localStorage = _localStorage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// strict mode | ||
'use strict' | ||
|
||
// internal object to hold data | ||
let _storage = {} | ||
|
||
/** | ||
* @param key key to item | ||
* @param {String} value key's value | ||
*/ | ||
const setItem = function (key, value) { | ||
_storage[key] = String(value) | ||
} | ||
|
||
/** | ||
* @return item or null if it does not exist | ||
*/ | ||
const getItem = function (key) { | ||
if (Object.prototype.hasOwnProperty.call(_storage, key)) { | ||
return _storage[key] | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* @return undefined | ||
*/ | ||
const removeItem = function (key) { | ||
if (Object.prototype.hasOwnProperty.call(_storage, key)) { | ||
delete _storage[key] | ||
} | ||
return undefined | ||
} | ||
|
||
/** | ||
* @return undefined | ||
*/ | ||
const clear = function () { | ||
_storage = {} | ||
} | ||
|
||
/** | ||
* Check if localStorage is supported by the browser | ||
* @return {Boolean} true if supported, false otherwise | ||
*/ | ||
const _isSupported = function () { | ||
// assume it's supported | ||
var supported = true | ||
|
||
try { | ||
// check if the browser allows access to local storage | ||
// it should be truthy, and it should be an object | ||
// ie window.localStorage is undefined | ||
if (!window.localStorage || !(typeof window.localStorage === 'object')) { | ||
supported = false | ||
} | ||
} catch (e) { | ||
// chrome and opera throw DOMException | ||
// safari throws SecurityError | ||
// edge throws Unspecified error | ||
supported = false | ||
} | ||
|
||
return supported | ||
} | ||
|
||
// local storage methods | ||
const _localStorage = { | ||
setItem: setItem, | ||
getItem: getItem, | ||
removeItem: removeItem, | ||
clear: clear | ||
} | ||
|
||
// export | ||
export { | ||
_localStorage, | ||
_isSupported, | ||
_storage | ||
} |
Oops, something went wrong.