nanoscope
Version:
A Lens Library for Javascript
541 lines (250 loc) • 9.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>nanoscope Class: PathLens</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.spacelab.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top ">
<div class="navbar-inner">
<a class="brand" href="index.html">nanoscope</a>
<ul class="nav">
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Compose.html">Compose</a>
</li>
<li>
<a href="Getter.html">Getter</a>
</li>
<li>
<a href="IndexedLens.html">IndexedLens</a>
</li>
<li>
<a href="IndexedLens.Unsafe.html">IndexedLens.Unsafe</a>
</li>
<li>
<a href="Lens.html">Lens</a>
</li>
<li>
<a href="MultiLens.html">MultiLens</a>
</li>
<li>
<a href="Optional.html">Optional</a>
</li>
<li>
<a href="PathLens.html">PathLens</a>
</li>
<li>
<a href="PathLens.Unsafe.html">PathLens.Unsafe</a>
</li>
<li>
<a href="Setter.html">Setter</a>
</li>
<li>
<a href="SliceLens.html">SliceLens</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="global.html#get">get</a>
</li>
<li>
<a href="global.html#IdLens">IdLens</a>
</li>
<li>
<a href="global.html#map">map</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span8">
<div id="main">
<h1 class="page-title">Class: PathLens</h1>
<section>
<header>
<h2>
PathLens
</h2>
</header>
<article>
<div class="container-overview">
<dt>
<h4 class="name" id="PathLens"><span class="type-signature"></span>new PathLens<span class="signature">(path, unsafe)</span><span class="type-signature"> → {<a href="Lens.html">Lens</a>}</span></h4>
</dt>
<dd>
<div class="description">
<p>A <code>PathLens</code> is a <code>Lens</code> that focuses on some element following a path in a JS object. They are safe by default
and will not throw errors when trying to get or set elements that don't exist. In regular javascript, for example:</p>
<pre class="prettyprint source lang-javascript"><code>{}.a.b; // Throws 'cannot read property b of undefined'</code></pre><p>But with a <code>PathLens</code>:</p>
<pre class="prettyprint source lang-javascript"><code>new PathLens('a.b').get(); // null</code></pre><p>You can also set values that don't exist yet:</p>
<pre class="prettyprint source lang-javascript"><code>new PathLens('a.b').set({}, 100); // {a : { b : 100 } }</code></pre><p>Unsafe <code>PathLenses</code> don't catch these errors and will throw the usual error messages. These can be constructed using
either the <code>PathLens.Unsafe</code> constructor or by setting the unsafe parameter in the <code>PathLens</code> constructor to <code>true</code>.</p>
</div>
<h5>Parameters:</h5>
<table class="params table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>path</code></td>
<td class="type">
<span class="param-type">string</span>
|
<span class="param-type">Array</span>
</td>
<td class="description last"><p>Array or dot-delimited string describing a path to follow in an object</p></td>
</tr>
<tr>
<td class="name"><code>unsafe</code></td>
<td class="type">
<span class="param-type">boolean</span>
</td>
<td class="description last"><p>If true, construct an unsafe version of</p></td>
</tr>
</tbody>
</table>
<dl class="details">
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><a href="Lens.html">Lens</a></span>
</dd>
</dl>
</dd>
</div>
<h3 class="subsection-title">Classes</h3>
<dl>
<dt><a href="PathLens.Unsafe.html">Unsafe</a></dt>
<dd></dd>
</dl>
<h3 class="subsection-title">Methods</h3>
<dl>
<dt>
<h4 class="name" id="deriveLenses"><span class="type-signature"><static> </span>deriveLenses<span class="signature">(obj)</span><span class="type-signature"></span></h4>
</dt>
<dd>
<div class="description">
<p><code>deriveLenses</code> derives <code>PathLens</code>es for every existent path in the object passed in.
For example, if you have the following structure:</p>
<pre class="prettyprint source lang-javascript"><code>var obj = { a: { b : { c : 8, d: 9 } } }</code></pre><p><code>PathLens.deriveLenses(obj)</code> gives back an object with the following keys:</p>
<p><code>'a', 'a.b', 'a.b.c', and 'a.b.d'</code>.</p>
<p>Each of these keys point to a safe <code>PathLens</code> for that path.</p>
</div>
<h5>Parameters:</h5>
<table class="params table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>obj</code></td>
<td class="type">
<span class="param-type">object</span>
</td>
<td class="description last"><p>The object to derive lenses from</p></td>
</tr>
</tbody>
</table>
<dl class="details">
</dl>
</dd>
</dl>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha11</a>
on 2014-12-17T09:37:57-05:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<div class="span3">
<div id="toc"></div>
</div>
<br clear="both">
</div>
</div>
<!--<script src="scripts/sunlight.js"></script>-->
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : "100px"
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
// $( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" );
$( ".tutorial-section pre, .readme-section pre" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
lang = "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : false,
showMenu : true,
enableDoclinks : true
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
</body>
</html>