UNPKG

obj-chain-core

Version:

fluent chaining for obj with dot-prop access

149 lines (115 loc) 4.42 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>plugins/SnapshotPlugin.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ObjChain.html">ObjChain</a><ul class='methods'><li data-type='method'><a href="ObjChain.html#.init">init</a></li><li data-type='method'><a href="ObjChain.html#escape">escape</a></li><li data-type='method'><a href="ObjChain.html#extend">extend</a></li><li data-type='method'><a href="ObjChain.html#has">has</a></li><li data-type='method'><a href="ObjChain.html#setIfNotEmpty">setIfNotEmpty</a></li><li data-type='method'><a href="ObjChain.html#toString">toString</a></li><li data-type='method'><a href="ObjChain.html#use">use</a></li><li data-type='method'><a href="ObjChain.html#val">val</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html"></a></li><li><a href="global.html#configPlugin">configPlugin</a></li><li><a href="global.html#flow">flow</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#set">set</a></li><li><a href="global.html#write">write</a></li></ul> </nav> <div id="main"> <h1 class="page-title">plugins/SnapshotPlugin.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>// use jest-like snapshots, can use with observable hooks... // const snapshots = {} const toarr = require('to-arr') const SnapshotPlugin = { init(thisArg) { thisArg.snapshots = { ast: {}, str: {}, } }, // --- /** * @private * @param {*} state * @return {*} code generated state */ getAst(state) { const {generate} = require('escodegen') const parsedState = generate(state) return parsedState }, /** * @param {String} [type='ast'] snapshot type to use * @param {Number} [index=1] index starting from last to use * @return {*} snapshot */ getSnapshot(type = 'ast', index = 1) { const store = this.snapshots[type] const keys = Object.keys(store) const keyIndex = keys.length - index const key = keys[keyIndex] const state = store[key] if (type === 'ast') return this.getAst(state) return state }, // --- /** * @private * @param {*} state * @param {number} [now=Date.now()] * @return {ObjChain} @chainable */ snapAst(state, now = Date.now()) { const lave = require('lave') const snap = lave(state) this.snapshots.ast[now] = snap return this }, /** * @private * @param {*} state * @param {number} [now=Date.now()] * @return {ObjChain} @chainable */ snapStr(state, now = Date.now()) { const stringify = require('javascript-stringify') const snap = stringify(state) this.snapshots.str[now] = snap return this }, /** * @param {Array&lt;string> | string} [types=['ast']] [ast, stringify] * @param {Array&lt;string> | string} [props=['data']] properties to snapshot * @return {ObjChain} @chainable */ snapshot(types = ['ast'], props = ['data']) { const now = Date.now() const state = {} toarr(props).forEach(prop => (state[prop] = this[prop])) toarr(types).forEach(type => { if (type === 'ast') this.snapAst(state, now) else this.snapStr(state, now) }) return this }, } module.exports = SnapshotPlugin </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Apr 28 2017 01:03:22 GMT-0700 (PDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>