init commit

This commit is contained in:
Geoff Doty 2019-06-24 14:45:14 -05:00
commit 8975cbd0bd
12 changed files with 5094 additions and 0 deletions

28
.eslintrc.json Normal file
View File

@ -0,0 +1,28 @@
{
"env": {
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2015
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

72
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,72 @@
# Contributing
So you want to contribute, nice. **Thank you**.
Bug reports and code and documentation patches are all welcome. You can help this project also by using the development version and by reporting any bugs you might encounter.
You may contribute in several ways like:
* Creating new features
* Fixing bugs
* Improving documentation and examples
* Translating any document here to your language
## Table of contents
* [Contributing](#contributing)
* [Developing](#developing)
* [Running tests](#running-tests)
* [Reporting a bug](#reporting-a-bug)
* [Request a feature](#request-a-feature)
* [Commit message](#commit-message)
* [Code style](#code-style)
## Developing
There is only one main source file in the project. It is the [/src/index.js](/src/index.js).
The [test/index.spec.js](test/index.spec.js) is for now the only unit test file in the project.
The `dist` includes the minified version of the source code.
## Running tests
Run unit tests using this command:
```bash
npm run test
```
## Reporting a bug
Use the [GitHub issue tracker](https://github.com/n2geoff/js-lib/issues) to report any bug you find.
Bugs description should include:
* How to reproduce the bug;
* Easy to understand title;
Would be nice to have some code showing how to reproduce the code, you may use [gist](https://gist.github.com) or [Codepen](https://codepen.io) for uploading your example code.
## Request a feature
Use the [GitHub issue tracker](https://github.com/n2geoff/js-lib/issues) to request a new feature.
Keep in mind, this is a pure javascript library
Feel free to port it to your favorite framework, such as [RiotJS](http://riotjs.com), Angular or VueJs in a new repository.
## Commit message
Commit messages should includes GitHub number reference and a imperative easy to understand sentence.
## Coding style
If it is supported in all major browers without transpiling, then please use those JavaScript language features in your code, with one caveat -- readablity is king.
Currently all ES5 and ES6/ES2015 are available.
This project is linted agaist [ESLint](https://eslint.org/) and the [`.eslintrc.json`](.eslintrc.json) is dead-simple, and all you need to followed.
Thank you for reading this.
Hey, **star** this *repo* and/or share it with your friends.

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

54
README.md Normal file
View File

@ -0,0 +1,54 @@
# State JS
> simple spa state managment utility
**WIP**
Simple state managment utility for single-page-applications w/ optional localStorage caching with a tiny footprint
> ALL values are JSONified
**Features**
- Tiny ~100 lines of JS
- Support both browser and NodeJS environments
- `.get` can be used as expression, `false` returned for all empty datatypes -- yes even `{}` & `[]`
## Quick Start
include the library *ONLY ONCE*
const state = require('state.js');
or in browser
<script src="state.js"></script>
### Options
`state` is a funciton that you can optionally pass in an `options` object
| Option | Descption |
| cache | enable localStorage |
| debug | display various console messages |
### API
from there you have access to a simple api, `get`, `set`, `remove`, and `clear`
| Method | Description |
| get(key) | get value by key |
| set(key, val) | set value by key |
| remove(key) | remove key |
| clear() | destories all state data |
## Support
Please open [an issue](http://code.negative9.net/geoff/state.js/issues/new) for support.
## Contributing
Anyone is welcome to contribute, however, if you decide to get involved, please take a moment to review the [guidelines](CONTRIBUTING.md), there minimalistic;)
## License
[MIT](LICENSE)

1
dist/state.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"sources":["state.js"],"names":["root","factory","define","amd","module","exports","state","this","opts","Object","assign","cache","debug","_state","_log","msg","console","log","get","key","val","length","getOwnPropertyNames","_has","JSON","parse","global","localStorage","getItem","set","value","setItem","stringify","remove","removeItem","clear"],"mappings":"CACC,SAASA,EAAMC,GAEU,mBAAXC,QAAyBA,OAAOC,IAEvCD,OAAO,CAAC,SAAUD,GACM,iBAAXG,QAAuBA,OAAOC,QAE3CD,OAAOC,QAAUJ,EAAQD,EAAKM,OAG9BN,EAAKM,MAAQL,EAAQD,EAAKM,OAVlC,CAaEC,KAAM,WACJ,aAiEA,OA/DA,SAAeC,GACXA,EAAOC,OAAOC,OAAO,CAACC,OAAO,EAAOC,OAAO,GAAQJ,GAAQ,IAE3D,IAAIK,EAAS,GAMb,SAASC,KAAQC,GACVP,EAAKI,OAEJI,QAAQC,IAAIF,GAqBpB,OA5BAD,EAAK,QAASD,GACdC,EAAK,SAAUN,EAAKI,OACpBE,EAAK,SAAUN,EAAKG,OA0Bb,CACHO,IAAMC,GACCX,EAAKG,MAnBhB,SAAcS,GACV,MAAkB,iBAARA,GAA4B,OAARA,EACjBA,IAAY,EAGH,iBAARA,EAEY,cAAfA,EAAIC,SACMD,EAAIC,QAASD,EAGdX,OAAOa,oBAAoBF,GAAKC,OAAS,GAAKD,OAN9D,EAeWG,CAAKC,KAAKC,MAAMC,OAAOC,aAAaC,QAAQT,KAE5CN,EAAOM,GAGtBU,IAAK,CAACV,EAAKW,KACJtB,EAAKG,MACJe,OAAOC,aAAaI,QAAQZ,EAAKK,KAAKQ,UAAUF,IAEhDjB,EAAOM,GAAOW,EAGXA,GAEXG,OAASd,GACFX,EAAKG,MACGe,OAAOC,aAAaO,WAAWf,GAE/BN,EAAOM,GAAO,KAG7BgB,MAAO","file":"state.js","sourcesContent":["/* state.js v{version} | MIT | https://github.com/n2geoff/state.js */\n(function(root, factory) {\n /* eslint-disable */\n if (typeof define === 'function' && define.amd) {\n // AMD\n define(['state'], factory);\n } else if(typeof module === 'object' && module.exports) {\n // CommonJS / NodeJS\n module.exports = factory(root.state);\n } else {\n // Global\n root.state = factory(root.state);\n }\n\n}(this, function() {\n 'use strict';\n\n function state(opts) {\n opts = Object.assign({cache: false, debug: false}, opts || {});\n\n let _state = {};\n\n _log('INIT:', _state);\n _log('DEBUG:', opts.debug);\n _log('CACHE:', opts.cache);\n\n function _log(...msg) {\n if(opts.debug) {\n /* eslint-disable */\n console.log(msg);\n }\n }\n\n function _has(val) {\n if(typeof val !== 'object' || val === null) {\n return !!val ? val : false;\n } else {\n\n if(typeof val === \"object\") {\n\n if(val.length !== 'undefined') {\n return !!val.length ? val : false;\n } else {\n\n return (Object.getOwnPropertyNames(val).length > 0) ? val : false;\n }\n }\n }\n }\n\n return {\n get: (key) => {\n if(opts.cache) {\n return _has(JSON.parse(global.localStorage.getItem(key)));\n } else {\n return _state[key];\n }\n },\n set: (key, value) => {\n if(opts.cache) {\n global.localStorage.setItem(key, JSON.stringify(value));\n } else {\n _state[key] = value;\n }\n\n return value;\n },\n remove: (key) => {\n if(opts.cache) {\n return global.localStorage.removeItem(key);\n } else {\n return _state[key] = null;\n }\n },\n clear: () => {}\n }\n\n };\n\n // Return it\n return state;\n}));\n"]}

3
dist/state.min.js vendored Normal file
View File

@ -0,0 +1,3 @@
/* state.js v0.5.0 | MIT | /http://code.negative9.net/geoff/state.js */
!function(e,t){"function"==typeof define&&define.amd?define(["state"],t):"object"==typeof module&&module.exports?module.exports=t(e.state):e.state=t(e.state)}(this,function(){"use strict";return function(e){e=Object.assign({cache:!1,debug:!1},e||{});let t={};function o(...t){e.debug&&console.log(t)}return o("INIT:",t),o("DEBUG:",e.debug),o("CACHE:",e.cache),{get:o=>e.cache?function(e){return"object"!=typeof e||null===e?e||!1:"object"==typeof e?"undefined"!==e.length?!!e.length&&e:Object.getOwnPropertyNames(e).length>0&&e:void 0}(JSON.parse(global.localStorage.getItem(o))):t[o],set:(o,n)=>(e.cache?global.localStorage.setItem(o,JSON.stringify(n)):t[o]=n,n),remove:o=>e.cache?global.localStorage.removeItem(o):t[o]=null,clear:()=>{}}}});
//# sourceMappingURL=state.js.map

15
gulpfile.js Normal file
View File

@ -0,0 +1,15 @@
'use strict';
const gulp = require('gulp'),
sourcemaps = require('gulp-sourcemaps'),
terser = require('gulp-terser');
// build riotjs tags (components)
gulp.task('default', () => {
return gulp.src('./src/state.js')
.pipe(sourcemaps.init())
.pipe(terser({keep_fnames: false}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist'));
});

4769
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

28
package.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "statejs",
"version": "0.7.0",
"description": "simple spa state managment utility",
"main": "src/index.js",
"directories": {
"doc": "docs",
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "node_modules/.bin/tape test/*.spec.js",
"lint": "npx eslint src/*.js",
"build": "npx gulp"
},
"keywords": [
"library"
],
"author": "Geoff Doty",
"license": "MIT",
"devDependencies": {
"eslint": "^5.16.0",
"gulp": "^4.0.2",
"gulp-sourcemaps": "^2.6.5",
"gulp-terser": "^1.2.0",
"tape": "^4.10.2"
}
}

82
src/state.js Normal file
View File

@ -0,0 +1,82 @@
/* state.js v0.5.0 | MIT | /http://code.negative9.net/geoff/state.js */
(function(root, factory) {
/* eslint-disable */
if (typeof define === 'function' && define.amd) {
// AMD
define(['state'], factory);
} else if(typeof module === 'object' && module.exports) {
// CommonJS / NodeJS
module.exports = factory(root.state);
} else {
// Global
root.state = factory(root.state);
}
}(this, function() {
'use strict';
function state(opts) {
opts = Object.assign({cache: false, debug: false}, opts || {});
let _state = {};
_log('INIT:', _state);
_log('DEBUG:', opts.debug);
_log('CACHE:', opts.cache);
function _log(...msg) {
if(opts.debug) {
/* eslint-disable */
console.log(msg);
}
}
function _has(val) {
if(typeof val !== 'object' || val === null) {
return !!val ? val : false;
} else {
if(typeof val === "object") {
if(val.length !== 'undefined') {
return !!val.length ? val : false;
} else {
return (Object.getOwnPropertyNames(val).length > 0) ? val : false;
}
}
}
}
return {
get: (key) => {
if(opts.cache) {
return _has(JSON.parse(global.localStorage.getItem(key)));
} else {
return _state[key];
}
},
set: (key, value) => {
if(opts.cache) {
global.localStorage.setItem(key, JSON.stringify(value));
} else {
_state[key] = value;
}
return value;
},
remove: (key) => {
if(opts.cache) {
return global.localStorage.removeItem(key);
} else {
return _state[key] = null;
}
},
clear: () => {}
}
};
// Return it
return state;
}));

22
test/index.spec.js Normal file
View File

@ -0,0 +1,22 @@
const test = require('tape');
const statejs = require('../src/state.js')({debug: true});
test('Index', function(t) {
var state = statejs;
t.test('setup', function(t) {
t.ok(state, 'state should be defined');
state.set('name', 'Geoff');
t.end();
});
t.test('state should have name', function(t) {
t.equal(state.get('name'), 'Geoff');
t.end();
});
});