Skip to content

Commit

Permalink
feat: transpile with babel for proper testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kubiak committed Oct 10, 2019
1 parent 345daae commit 3d25cc5
Show file tree
Hide file tree
Showing 11 changed files with 4,486 additions and 279 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage
node_modules
test/test-all.js
dist
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Add to your project:

`npm i local-storage-poorlyfill`

```html
<script type="text/javascript" src="node_modules/local-storage-poorlyfill/dist/local-storage-poorlyfill.js"></script>
<script>
// use _localStorage
</script>
```

### Why?

- For local files, IE and Edge do not have support for local storage
Expand Down
82 changes: 70 additions & 12 deletions gulpfile.js
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/'))
}

/**
Expand All @@ -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
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function (config) {

// list of files / patterns to load in the browser
files: [
'test/test-all.js'
'dist/test.js'
],

// list of files / patterns to exclude
Expand All @@ -23,7 +23,7 @@ module.exports = function (config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/test-all.js': 'coverage'
'dist/test.js': 'coverage'
},

// test results reporter to use
Expand Down
117 changes: 0 additions & 117 deletions local-storage-poorlyfill.js

This file was deleted.

18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "local-storage-poorlyfill",
"version": "0.0.0-development",
"description": "in-memory polyfill for local storage",
"main": "\"\"",
"main": "src/local-storage-poorlyfill.js",
"directories": {
"dist": "dist",
"src": "src",
"test": "test"
},
"scripts": {
Expand All @@ -26,10 +28,18 @@
"url": "https://github.com/patkub/local-storage-poorlyfill/issues"
},
"homepage": "https://github.com/patkub/local-storage-poorlyfill#readme",
"dependencies": {},
"dependencies": {
"@babel/polyfill": "^7.6.0",
"gulp-babel": "^8.0.0",
"gulp-rollup": "^2.16.2"
},
"devDependencies": {
"@babel/cli": "^7.6.3",
"@babel/core": "^7.6.3",
"@babel/preset-env": "^7.6.3",
"chai": "^4.2.0",
"gulp": "^4.0.2",
"gulp-header": "^2.0.9",
"gulp-include": "^2.4.1",
"gulp-rename": "^1.4.0",
"karma": "^4.3.0",
Expand All @@ -42,6 +52,8 @@
"mocha": "^6.2.1",
"semantic-release": "^15.13.24",
"sinon": "^7.5.0",
"standard": "^14.3.1"
"standard": "^14.3.1",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
}
}
11 changes: 11 additions & 0 deletions src/local-storage-poorlyfill.js
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
}
80 changes: 80 additions & 0 deletions src/local-storage.js
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
}
Loading

0 comments on commit 3d25cc5

Please sign in to comment.