Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | <?xml version="1.0" encoding="utf-8" ?> |
2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
3 | ||
4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
5 | <head> | |
6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
7 | <meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" /> | |
8 | <title>MochiKit.Base - functional programming and useful comparisons</title> | |
9 | ||
10 | <link rel="stylesheet" href="../../../include/css/documentation.css" type="text/css" /> | |
11 | <script type="text/javascript" src="../../../packed/MochiKit/MochiKit.js"></script> | |
12 | <script type="text/javascript" src="../../js/toc.js"></script> | |
13 | </head> | |
14 | <body> | |
15 | <div class="document"> | |
16 | <div class="section"> | |
17 | <h1><a id="name" name="name">Name</a></h1> | |
18 | <p>MochiKit.Base - functional programming and useful comparisons</p> | |
19 | </div> | |
20 | <div class="section"> | |
21 | <h1><a id="synopsis" name="synopsis">Synopsis</a></h1> | |
22 | <pre class="literal-block"> | |
23 | myObjectRepr = function () { | |
24 | // gives a nice, stable string representation for objects, | |
25 | // ignoring any methods | |
26 | var keyValuePairs = []; | |
27 | for (var k in this) { | |
28 | var v = this[k]; | |
29 | if (typeof(v) != 'function') { | |
30 | keyValuePairs.push([k, v]); | |
31 | } | |
32 | }; | |
33 | keyValuePairs.sort(compare); | |
34 | return "{" + map( | |
35 | function (pair) { | |
36 | return map(repr, pair).join(":"); | |
37 | }, | |
38 | keyValuePairs | |
39 | ).join(", ") + "}"; | |
40 | }; | |
41 | ||
42 | // repr() will look for objects that have a repr method | |
43 | myObjectArray = [ | |
44 | {"a": 3, "b": 2, "repr": myObjectRepr}, | |
45 | {"a": 1, "b": 2, "repr": myObjectRepr} | |
46 | ]; | |
47 | ||
48 | // sort it by the "a" property, check to see if it matches | |
49 | myObjectArray.sort(keyComparator("a")); | |
50 | expectedRepr = '[{"a": 1, "b": 2}, {"a": 3, "b": 2}]'; | |
51 | assert( repr(myObjectArray) == expectedRepr ); | |
52 | ||
53 | // get just the "a" values out into an array | |
54 | sortedAValues = map(itemgetter("a"), myObjectArray); | |
55 | assert( compare(sortedAValues, [1, 3]) == 0 ); | |
56 | ||
57 | // serialize an array as JSON, unserialize it, expect something equivalent | |
58 | myArray = [1, 2, "3", null, undefined]; | |
59 | assert( objEqual(evalJSON(serializeJSON(myArray)), myArray) ); | |
60 | </pre> | |
61 | </div> | |
62 | <div class="section"> | |
63 | <h1><a id="description" name="description">Description</a></h1> | |
64 | <p><a class="mochiref reference" href="Base.html">MochiKit.Base</a> is the foundation for the MochiKit suite. | |
65 | It provides:</p> | |
66 | <ul class="simple"> | |
67 | <li>An extensible comparison facility | |
68 | (<a class="mochiref reference" href="#fn-compare">compare</a>, <a class="mochiref reference" href="#fn-registercomparator">registerComparator</a>)</li> | |
69 | <li>An extensible programmer representation facility | |
70 | (<a class="mochiref reference" href="#fn-repr">repr</a>, <a class="mochiref reference" href="#fn-registerrepr">registerRepr</a>)</li> | |
71 | <li>An extensible JSON <a class="footnote-reference" href="#id7" id="id1" name="id1">[1]</a> serialization and evaluation facility | |
72 | (<a class="mochiref reference" href="#fn-serializejson">serializeJSON</a>, <a class="mochiref reference" href="#fn-evaljson">evalJSON</a>, | |
73 | <a class="mochiref reference" href="#fn-registerjson">registerJSON</a>)</li> | |
74 | <li>A simple adaptation facility (<a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a>)</li> | |
75 | <li>Convenience functions for manipulating objects and Arrays | |
76 | (<a class="mochiref reference" href="#fn-update">update</a>, <a class="mochiref reference" href="#fn-setdefault">setdefault</a>, <a class="mochiref reference" href="#fn-extend">extend</a>, etc.)</li> | |
77 | <li>Array-based functional programming | |
78 | (<a class="mochiref reference" href="#fn-map">map</a>, <a class="mochiref reference" href="#fn-filter">filter</a>, etc.)</li> | |
79 | <li>Bound and partially applied functions | |
80 | (<a class="mochiref reference" href="#fn-bind">bind</a>, <a class="mochiref reference" href="#fn-method">method</a>, <a class="mochiref reference" href="#fn-partial">partial</a>)</li> | |
81 | </ul> | |
82 | <p>Python users will feel at home with <a class="mochiref reference" href="Base.html">MochiKit.Base</a>, as the | |
83 | facilities are quite similar to those available as part of Python and | |
84 | the Python standard library.</p> | |
85 | </div> | |
86 | <div class="section"> | |
87 | <h1><a id="dependencies" name="dependencies">Dependencies</a></h1> | |
88 | <p>None.</p> | |
89 | </div> | |
90 | <div class="section"> | |
91 | <h1><a id="overview" name="overview">Overview</a></h1> | |
92 | <div class="section"> | |
93 | <h2><a id="comparison" name="comparison">Comparison</a></h2> | |
94 | <p>The comparators (operators for comparison) in JavaScript are deeply | |
95 | broken, and it is not possible to teach them new tricks.</p> | |
96 | <p>MochiKit exposes an extensible comparison facility as a simple | |
97 | <a class="mochiref reference" href="#fn-compare">compare(a, b)</a> function, which should be used in lieu of | |
98 | JavaScript's operators whenever comparing objects other than numbers | |
99 | or strings (though you can certainly use <a class="mochiref reference" href="#fn-compare">compare</a> for | |
100 | those, too!).</p> | |
101 | <p>The <a class="mochiref reference" href="#fn-compare">compare</a> function has the same signature and return | |
102 | value as a sort function for <tt class="docutils literal"><span class="pre">Array.prototype.sort</span></tt>, and is often | |
103 | used in that context.</p> | |
104 | <p>Defining new comparators for the <a class="mochiref reference" href="#fn-compare">compare</a> function to use | |
105 | is done by adding an entry to its <a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a> with the | |
106 | <a class="mochiref reference" href="#fn-registercomparator">registerComparator</a> function.</p> | |
107 | </div> | |
108 | <div class="section"> | |
109 | <h2><a id="programmer-representation" name="programmer-representation">Programmer Representation</a></h2> | |
110 | <p>JavaScript's default representation mechanism, <tt class="docutils literal"><span class="pre">toString</span></tt>, is | |
111 | notorious for having terrible default behavior. It's also very unwise | |
112 | to change that default, as other JavaScript code you may be using may | |
113 | depend on it.</p> | |
114 | <p>It's also useful to separate the concept of a "string representation" | |
115 | and a "string representation for programmers", much like Python does | |
116 | with its str and repr protocols.</p> | |
117 | <p><a class="mochiref reference" href="#fn-repr">repr</a> provides this programmer representation for | |
118 | JavaScript, in a way that doesn't require object prototype hacking: | |
119 | using an <a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a>.</p> | |
120 | <p>Objects that implement the repr protocol can either implement a | |
121 | <tt class="docutils literal"><span class="pre">.repr()</span></tt> or <tt class="docutils literal"><span class="pre">.__repr__()</span></tt> method, or they can simply have an | |
122 | adapter setup to generate programmer representations. By default, the | |
123 | registry provides nice representations for <tt class="docutils literal"><span class="pre">null</span></tt>, <tt class="docutils literal"><span class="pre">undefined</span></tt>, | |
124 | <tt class="docutils literal"><span class="pre">Array</span></tt>, and objects or functions with a <tt class="docutils literal"><span class="pre">NAME</span></tt> attribute that use | |
125 | the default <tt class="docutils literal"><span class="pre">toString</span></tt>. For objects that <tt class="docutils literal"><span class="pre">repr</span></tt> doesn't already | |
126 | understand, it simply defaults to <tt class="docutils literal"><span class="pre">toString</span></tt>, so it will integrate | |
127 | seamlessly with code that implements the idiomatic JavaScript | |
128 | <tt class="docutils literal"><span class="pre">toString</span></tt> method!</p> | |
129 | <p>To define a programmer representation for your own objects, simply add | |
130 | a <tt class="docutils literal"><span class="pre">.repr()</span></tt> or <tt class="docutils literal"><span class="pre">.__repr__()</span></tt> method that returns a string. For | |
131 | objects that you didn't create (e.g., from a script you didn't write, | |
132 | or a built-in object), it is instead recommended that you create an | |
133 | adapter with <a class="mochiref reference" href="#fn-registerrepr">registerRepr</a>.</p> | |
134 | </div> | |
135 | <div class="section"> | |
136 | <h2><a id="json-serialization" name="json-serialization">JSON Serialization</a></h2> | |
137 | <p>JSON <a class="footnote-reference" href="#id7" id="id2" name="id2">[1]</a>, JavaScript Object Notation, is a widely used serialization | |
138 | format in the context of web development. It's extremely simple, | |
139 | lightweight, and fast. In its essence, JSON is a restricted subset of | |
140 | JavaScript syntax suitable for sending over the wire that can be | |
141 | unserialized with a simple eval. It's often used as an alternative to | |
142 | XML in "AJAX" contexts because it is compact, fast, and much simpler | |
143 | to use for most purposes.</p> | |
144 | <p>To create a JSON serialization of any object, simply call | |
145 | <a class="mochiref reference" href="#fn-serializejson">serializeJSON()</a> with that object. To unserialize a JSON | |
146 | string, simply call <a class="mochiref reference" href="#fn-evaljson">evalJSON()</a> with the serialization.</p> | |
147 | <p>In order of precedence, <a class="mochiref reference" href="#fn-serializejson">serializeJSON</a> coerces the given | |
148 | argument into a JSON serialization:</p> | |
149 | <ol class="arabic simple"> | |
150 | <li>Primitive types are returned as their JSON representation: | |
151 | <tt class="docutils literal"><span class="pre">string</span></tt>, <tt class="docutils literal"><span class="pre">number</span></tt>, <tt class="docutils literal"><span class="pre">boolean</span></tt>, <tt class="docutils literal"><span class="pre">null</span></tt>.</li> | |
152 | <li>If the object has a <tt class="docutils literal"><span class="pre">__json__</span></tt> or <tt class="docutils literal"><span class="pre">json</span></tt> method, then it is | |
153 | called with no arguments. If the result of this method is not the | |
154 | object itself, then the new object goes through rule processing | |
155 | again (e.g. it may return a string, which is then serialized in | |
156 | JSON format).</li> | |
157 | <li>If the object is <tt class="docutils literal"><span class="pre">Array</span></tt>-like (has a <tt class="docutils literal"><span class="pre">length</span></tt> property that is | |
158 | a number, and is not a function), then it is serialized as a JSON | |
159 | array. Each element will be processed according to these rules in | |
160 | order. Elements that can not be serialized (e.g. functions) will | |
161 | be replaced with <tt class="docutils literal"><span class="pre">undefined</span></tt>.</li> | |
162 | <li>The <tt class="docutils literal"><span class="pre">jsonRegistry</span></tt> <a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a> is consulted for | |
163 | an adapter for this object. JSON adapters take one argument (the | |
164 | object), and are expected to behave like a <tt class="docutils literal"><span class="pre">__json__</span></tt> or | |
165 | <tt class="docutils literal"><span class="pre">json</span></tt> method (return another object to be serialized, or | |
166 | itself).</li> | |
167 | <li>If the object is <tt class="docutils literal"><span class="pre">undefined</span></tt>, a <tt class="docutils literal"><span class="pre">TypeError</span></tt> is thrown. If you | |
168 | wish to serialize <tt class="docutils literal"><span class="pre">undefined</span></tt> as <tt class="docutils literal"><span class="pre">null</span></tt> or some other value, you | |
169 | should create an adapter to do so.</li> | |
170 | <li>If no adapter is available, the object is enumerated and | |
171 | serialized as a JSON object (name:value pairs). All names are | |
172 | expected to be strings. Each value is serialized according to | |
173 | these rules, and if it can not be serialized (e.g. methods), then | |
174 | that name:value pair will be skipped.</li> | |
175 | </ol> | |
176 | </div> | |
177 | <div class="section"> | |
178 | <h2><a id="adapter-registries" name="adapter-registries">Adapter Registries</a></h2> | |
179 | <p>MochiKit makes extensive use of adapter registries, which enable you | |
180 | to implement object-specific behaviors for objects that you do not | |
181 | necessarily want to modify, such as built-in objects. This is | |
182 | especially useful because JavaScript does not provide a method for | |
183 | hiding user-defined properties from <tt class="docutils literal"><span class="pre">for</span> <span class="pre">propName</span> <span class="pre">in</span> <span class="pre">obj</span></tt> | |
184 | enumeration.</p> | |
185 | <p><a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a> is simply an encapsulation for an ordered | |
186 | list of "check" and "wrap" function pairs. Each | |
187 | <a class="mochiref reference" href="#fn-adapterregistry">AdapterRegistry</a> instance should perform one function, but | |
188 | may have multiple ways to achieve that function based upon the | |
189 | arguments. One way to think of it is as a poor man's generic function, | |
190 | or multiple dispatch (on arbitrary functions, not just type!).</p> | |
191 | <p>Check functions take one or more arguments, and return <tt class="docutils literal"><span class="pre">true</span></tt> if the | |
192 | argument list is suitable for the wrap function. Check functions | |
193 | should perform "cheap" checks of an object's type or contents, before | |
194 | the "expensive" wrap function is called.</p> | |
195 | <p>Wrap functions take the same arguments as check functions and do some | |
196 | operation, such as creating a programmer representation or comparing | |
197 | both arguments.</p> | |
198 | </div> | |
199 | <div class="section"> | |
200 | <h2><a id="convenience-functions" name="convenience-functions">Convenience Functions</a></h2> | |
201 | <p>Much of <a class="mochiref reference" href="Base.html">MochiKit.Base</a> is there to simply remove the grunt | |
202 | work of doing generic JavaScript programming.</p> | |
203 | <p>Need to take every property from one object and set them on another? | |
204 | No problem, just call <a class="mochiref reference" href="#fn-update">update(dest, src)</a>! What if you just | |
205 | wanted to update keys that weren't already set? Look no further than | |
206 | <a class="mochiref reference" href="#fn-setdefault">setdefault(dest, src[, ...])</a>.</p> | |
207 | <p>Want to return a mutable object, but don't want to suffer the | |
208 | consequences if the user mutates it? Just <a class="mochiref reference" href="#fn-clone">clone(it)</a> and | |
209 | you'll get a copy-on-write clone. Cheaper than a copy!</p> | |
210 | <p>Need to extend an <tt class="docutils literal"><span class="pre">Array</span></tt> with another array? Or even an | |
211 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like object such as a <tt class="docutils literal"><span class="pre">NodeList</span></tt> or the special | |
212 | <tt class="docutils literal"><span class="pre">arguments</span></tt> object? Even if you need to skip the first few elements | |
213 | of the source <tt class="docutils literal"><span class="pre">Array</span></tt>-like object, it's no problem with | |
214 | <a class="mochiref reference" href="#fn-extend">extend(dstArray, srcArrayLike[, skip])</a>!</p> | |
215 | <p>Wouldn't it be convenient to have all of the JavaScript operators were | |
216 | available as functions somewhere? That's what the | |
217 | <a class="mochiref reference" href="#fn-operators">operators</a> table is for, and it even comes with additional | |
218 | operators based on the <a class="mochiref reference" href="#fn-compare">compare</a> function.</p> | |
219 | <p>Need to walk some tree of objects and manipulate or find something in | |
220 | it? A DOM element tree perhaps? Use <a class="mochiref reference" href="#fn-nodewalk">nodeWalk(node, | |
221 | visitor)</a>!</p> | |
222 | <p>There's plenty more, so check out the <a class="reference" href="#api-reference">API Reference</a> below.</p> | |
223 | </div> | |
224 | <div class="section"> | |
225 | <h2><a id="functional-programming" name="functional-programming">Functional Programming</a></h2> | |
226 | <p>Functional programming constructs such as <a class="mochiref reference" href="#fn-map">map</a> and | |
227 | <a class="mochiref reference" href="#fn-filter">filter</a> can save you a lot of time, because JavaScript | |
228 | iteration is error-prone and arduous. Writing less code is the best | |
229 | way to prevent bugs, and functional programming can help you do that.</p> | |
230 | <p><a class="mochiref reference" href="Base.html">MochiKit.Base</a> ships with a few simple Array-based | |
231 | functional programming constructs, namely <a class="mochiref reference" href="#fn-map">map</a> and | |
232 | <a class="mochiref reference" href="#fn-filter">filter</a>, and their "extended" brethren, <a class="mochiref reference" href="#fn-xmap">xmap</a> | |
233 | and <a class="mochiref reference" href="#fn-xfilter">xfilter</a>.</p> | |
234 | <p><a class="mochiref reference" href="#fn-map">map(func, arrayLike[, ...])</a> takes a function and an | |
235 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like object, and creates a new <tt class="docutils literal"><span class="pre">Array</span></tt>. The new <tt class="docutils literal"><span class="pre">Array</span></tt> | |
236 | is the result of <tt class="docutils literal"><span class="pre">func(element)</span></tt> for every element of <tt class="docutils literal"><span class="pre">arrayLike</span></tt>, | |
237 | much like the <tt class="docutils literal"><span class="pre">Array.prototype.map</span></tt> extension in Mozilla. However, | |
238 | <a class="mochiref reference" href="Base.html">MochiKit.Base</a> takes that a step further and gives you the | |
239 | full blown Python version of <a class="mochiref reference" href="#fn-map">map</a>, which will take several | |
240 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like objects, and calls the function with one argument per | |
241 | given <tt class="docutils literal"><span class="pre">Array</span></tt>-like, e.g.:</p> | |
242 | <pre class="literal-block"> | |
243 | var arrayOne = [1, 2, 3, 4, 5]; | |
244 | var arrayTwo = [1, 5, 2, 4, 3]; | |
245 | var arrayThree = [5, 2, 1, 3, 4]; | |
246 | var biggestElements = map(objMax, arrayOne, arrayTwo, arrayThree); | |
247 | assert( objEqual(biggestElements, [5, 5, 3, 4, 5]) ); | |
248 | </pre> | |
249 | <p><a class="mochiref reference" href="#fn-filter">filter(func, arrayLike[, self])</a> takes a function and an | |
250 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like object, and returns a new <tt class="docutils literal"><span class="pre">Array</span></tt>. This is basically | |
251 | identical to the <tt class="docutils literal"><span class="pre">Array.prototype.filter</span></tt> extension in | |
252 | Mozilla. self, if given, will be used as <tt class="docutils literal"><span class="pre">this</span></tt> in the context of | |
253 | func when called.</p> | |
254 | <p><a class="mochiref reference" href="#fn-xmap">xmap</a> and <a class="mochiref reference" href="#fn-xfilter">xfilter</a> are just special forms of | |
255 | <a class="mochiref reference" href="#fn-map">map</a> and <a class="mochiref reference" href="#fn-filter">filter</a> that accept a function as the | |
256 | first argument, and use the extra arguments as the <tt class="docutils literal"><span class="pre">Array</span></tt>-like. Not | |
257 | terribly interesting, but a definite time-saver in some cases.</p> | |
258 | <p>If you appreciate the functional programming facilities here, you | |
259 | should definitely check out <a class="mochiref reference" href="Iter.html">MochiKit.Iter</a>, which provides | |
260 | full blown iterators, <a class="mochiref reference" href="Iter.html#fn-range">MochiKit.Iter.range</a>, | |
261 | <a class="mochiref reference" href="Iter.html#fn-reduce">MochiKit.Iter.reduce</a>, and a near-complete port of Python's | |
262 | itertools <a class="footnote-reference" href="#id8" id="id3" name="id3">[2]</a> module, with some extra stuff thrown in for good | |
263 | measure!</p> | |
264 | </div> | |
265 | <div class="section"> | |
266 | <h2><a id="bound-and-partial-functions" name="bound-and-partial-functions">Bound and Partial Functions</a></h2> | |
267 | <p>JavaScript's method-calling special form and lack of bound functions | |
268 | (functions that know what <tt class="docutils literal"><span class="pre">this</span></tt> should be) are one of the first | |
269 | stumbling blocks that programmers new to JavaScript face. The | |
270 | <a class="mochiref reference" href="#fn-bind">bind(func, self)</a> method fixes that right up by returning a | |
271 | new function that calls func with the right <tt class="docutils literal"><span class="pre">this</span></tt>.</p> | |
272 | <p>In order to take real advantage of all this fancy functional | |
273 | programming stuff, you're probably going to want partial | |
274 | application. This allows you to create a new function from an existing | |
275 | function that remembers some of the arguments. For example, if you | |
276 | wanted to compare a given object to a slew of other objects, you could | |
277 | do something like this:</p> | |
278 | <pre class="literal-block"> | |
279 | compareWithOne = partial(compare, 1); | |
280 | results = map(compareWithOne, [0, 1, 2, 3]); | |
281 | assert( objEqual(results, [-1, 0, 1, 1]) ); | |
282 | </pre> | |
283 | <p>One of the better uses of partial functions is in | |
284 | <a class="mochiref reference" href="DOM.html">MochiKit.DOM</a>, which is certainly a must-see for those of | |
285 | you creating lots of DOM elements with JavaScript!</p> | |
286 | </div> | |
287 | </div> | |
288 | <div class="section"> | |
289 | <h1><a id="api-reference" name="api-reference">API Reference</a></h1> | |
290 | <div class="section"> | |
291 | <h2><a id="errors" name="errors">Errors</a></h2> | |
292 | <p> | |
293 | <a name="fn-notfound"></a> | |
294 | <a class="mochidef reference" href="#fn-notfound">NotFound</a>:</p> | |
295 | <blockquote> | |
296 | <p>A singleton error raised when no suitable adapter is found</p> | |
297 | <dl class="docutils"> | |
298 | <dt><em>Availability</em>:</dt> | |
299 | <dd>Available in MochiKit 1.3.1+</dd> | |
300 | </dl> | |
301 | </blockquote> | |
302 | </div> | |
303 | <div class="section"> | |
304 | <h2><a id="constructors" name="constructors">Constructors</a></h2> | |
305 | <p> | |
306 | <a name="fn-adapterregistry"></a> | |
307 | <a class="mochidef reference" href="#fn-adapterregistry">AdapterRegistry</a>:</p> | |
308 | <blockquote> | |
309 | <p>A registry to facilitate adaptation.</p> | |
310 | <p>All <tt class="docutils literal"><span class="pre">check</span></tt>/<tt class="docutils literal"><span class="pre">wrap</span></tt> function pairs in a given registry should | |
311 | take the same number of arguments.</p> | |
312 | <dl class="docutils"> | |
313 | <dt><em>Availability</em>:</dt> | |
314 | <dd>Available in MochiKit 1.3.1+</dd> | |
315 | </dl> | |
316 | </blockquote> | |
317 | <p> | |
318 | <a name="fn-adapterregistry.prototype.register"></a> | |
319 | <a class="mochidef reference" href="#fn-adapterregistry.prototype.register">AdapterRegistry.prototype.register(name, check, wrap[, override])</a>:</p> | |
320 | <blockquote> | |
321 | <dl class="docutils"> | |
322 | <dt><tt class="docutils literal"><span class="pre">name</span></tt>:</dt> | |
323 | <dd>a unique identifier used to identify this adapter so that it | |
324 | may be unregistered.</dd> | |
325 | <dt><tt class="docutils literal"><span class="pre">check</span></tt>:</dt> | |
326 | <dd>function that should return <tt class="docutils literal"><span class="pre">true</span></tt> if the given arguments | |
327 | are appropriate for the <tt class="docutils literal"><span class="pre">wrap</span></tt> function.</dd> | |
328 | <dt><tt class="docutils literal"><span class="pre">wrap</span></tt>:</dt> | |
329 | <dd>function that takes the same parameters as <tt class="docutils literal"><span class="pre">check</span></tt> and does | |
330 | the adaptation. Every <tt class="docutils literal"><span class="pre">wrap</span></tt>/<tt class="docutils literal"><span class="pre">check</span></tt> function pair in the | |
331 | registry should have the same number of arguments.</dd> | |
332 | <dt><tt class="docutils literal"><span class="pre">override</span></tt>:</dt> | |
333 | <dd>if <tt class="docutils literal"><span class="pre">true</span></tt>, the <tt class="docutils literal"><span class="pre">check</span></tt> function will be | |
334 | given highest priority. Otherwise, the lowest.</dd> | |
335 | <dt><em>Availability</em>:</dt> | |
336 | <dd>Available in MochiKit 1.3.1+</dd> | |
337 | </dl> | |
338 | </blockquote> | |
339 | <p> | |
340 | <a name="fn-adapterregistry.prototype.match"></a> | |
341 | <a class="mochidef reference" href="#fn-adapterregistry.prototype.match">AdapterRegistry.prototype.match(obj[, ...])</a>:</p> | |
342 | <blockquote> | |
343 | <p>Find an adapter for the given arguments by calling every <tt class="docutils literal"><span class="pre">check</span></tt> | |
344 | function until one returns <tt class="docutils literal"><span class="pre">true</span></tt>.</p> | |
345 | <p>If no suitable adapter is found, throws <a class="mochiref reference" href="#fn-notfound">NotFound</a>.</p> | |
346 | <dl class="docutils"> | |
347 | <dt><em>Availability</em>:</dt> | |
348 | <dd>Available in MochiKit 1.3.1+</dd> | |
349 | </dl> | |
350 | </blockquote> | |
351 | <p> | |
352 | <a name="fn-adapterregistry.prototype.unregister"></a> | |
353 | <a class="mochidef reference" href="#fn-adapterregistry.prototype.unregister">AdapterRegistry.prototype.unregister(name)</a>:</p> | |
354 | <blockquote> | |
355 | <p>Remove a named adapter from the registry</p> | |
356 | <dl class="docutils"> | |
357 | <dt><em>Availability</em>:</dt> | |
358 | <dd>Available in MochiKit 1.3.1+</dd> | |
359 | </dl> | |
360 | </blockquote> | |
361 | <p> | |
362 | <a name="fn-namederror"></a> | |
363 | <a class="mochidef reference" href="#fn-namederror">NamedError</a>:</p> | |
364 | <blockquote> | |
365 | <p>Convenience constructor for creating new errors | |
366 | (e.g. <a class="mochiref reference" href="#fn-notfound">NotFound</a>)</p> | |
367 | <dl class="docutils"> | |
368 | <dt><em>Availability</em>:</dt> | |
369 | <dd>Available in MochiKit 1.3.1+</dd> | |
370 | </dl> | |
371 | </blockquote> | |
372 | </div> | |
373 | <div class="section"> | |
374 | <h2><a id="functions" name="functions">Functions</a></h2> | |
375 | <p> | |
376 | <a name="fn-arrayequal"></a> | |
377 | <a class="mochidef reference" href="#fn-arrayequal">arrayEqual(self, arr)</a>:</p> | |
378 | <blockquote> | |
379 | <p>Compare the arrays <tt class="docutils literal"><span class="pre">self</span></tt> and <tt class="docutils literal"><span class="pre">arr</span></tt> for equality using | |
380 | <tt class="docutils literal"><span class="pre">compare</span></tt> on each element. Uses a fast-path for length | |
381 | differences.</p> | |
382 | <dl class="docutils"> | |
383 | <dt><em>Availability</em>:</dt> | |
384 | <dd>Available in MochiKit 1.3.1+</dd> | |
385 | </dl> | |
386 | </blockquote> | |
387 | <p> | |
388 | <a name="fn-average"></a> | |
389 | <a class="mochidef reference" href="#fn-average">average(lst[, ...])</a>:</p> | |
390 | <blockquote> | |
391 | <p>This function is an alias of <a class="mochiref reference" href="#fn-mean">mean()</a>.</p> | |
392 | <dl class="docutils"> | |
393 | <dt><em>Availability</em>:</dt> | |
394 | <dd>Available in MochiKit 1.3.1+</dd> | |
395 | </dl> | |
396 | </blockquote> | |
397 | <p> | |
398 | <a name="fn-bind"></a> | |
399 | <a class="mochidef reference" href="#fn-bind">bind(func, self[, arg, ...])</a>:</p> | |
400 | <blockquote> | |
401 | <p>Return a copy of <tt class="docutils literal"><span class="pre">func</span></tt> bound to <tt class="docutils literal"><span class="pre">self</span></tt>. This means whenever | |
402 | and however the returned function is called, <tt class="docutils literal"><span class="pre">this</span></tt> will always | |
403 | reference the given <tt class="docutils literal"><span class="pre">self</span></tt>. <tt class="docutils literal"><span class="pre">func</span></tt> may be either a function | |
404 | object, or a string. If it is a string, then <tt class="docutils literal"><span class="pre">self[func]</span></tt> will | |
405 | be used, making these two statements equivalent:</p> | |
406 | <pre class="literal-block"> | |
407 | bind("method", self); | |
408 | bind(self.method, self); | |
409 | </pre> | |
410 | <p>Calling <a class="mochiref reference" href="#fn-bind">bind(func, self)</a> on an already bound function | |
411 | will return a new function that is bound to the new <tt class="docutils literal"><span class="pre">self</span></tt>! If | |
412 | <tt class="docutils literal"><span class="pre">self</span></tt> is <tt class="docutils literal"><span class="pre">undefined</span></tt>, then the previous <tt class="docutils literal"><span class="pre">self</span></tt> is used. If | |
413 | <tt class="docutils literal"><span class="pre">self</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, then the <tt class="docutils literal"><span class="pre">this</span></tt> object is used (which may | |
414 | or may not be the global object). To force binding to the global | |
415 | object, you should pass it explicitly.</p> | |
416 | <p>Additional arguments, if given, will be partially applied to the | |
417 | function. These three expressions are equivalent and return | |
418 | equally efficient functions (<a class="mochiref reference" href="#fn-bind">bind</a> and | |
419 | <a class="mochiref reference" href="#fn-partial">partial</a> share the same code path):</p> | |
420 | <ul class="simple"> | |
421 | <li><a class="mochiref reference" href="#fn-bind">bind(oldfunc, self, arg1, arg2)</a></li> | |
422 | <li><a class="mochiref reference" href="#fn-bind">bind(partial(oldfunc, arg1, arg2), self)</a></li> | |
423 | <li><a class="mochiref reference" href="#fn-partial">partial(bind(oldfunc, self), arg1, arg2)</a></li> | |
424 | </ul> | |
425 | <dl class="docutils"> | |
426 | <dt><em>Availability</em>:</dt> | |
427 | <dd>Available in MochiKit 1.3.1+</dd> | |
428 | </dl> | |
429 | </blockquote> | |
430 | <p> | |
431 | <a name="fn-bindmethods"></a> | |
432 | <a class="mochidef reference" href="#fn-bindmethods">bindMethods(self)</a>:</p> | |
433 | <blockquote> | |
434 | <p>Replace all functions <tt class="docutils literal"><span class="pre">meth</span></tt> on <tt class="docutils literal"><span class="pre">self</span></tt> with | |
435 | <a class="mochiref reference" href="#fn-bind">bind(meth, self)</a>. This emulates Python's bound | |
436 | instance methods, where there is no need to worry about preserving | |
437 | <tt class="docutils literal"><span class="pre">this</span></tt> when the method is used as a callback.</p> | |
438 | <dl class="docutils"> | |
439 | <dt><em>Availability</em>:</dt> | |
440 | <dd>Available in MochiKit 1.3.1+</dd> | |
441 | </dl> | |
442 | </blockquote> | |
443 | <p> | |
444 | <a name="fn-camelize"></a> | |
445 | <a class="mochidef reference" href="#fn-camelize">camelize(str)</a>:</p> | |
446 | <blockquote> | |
447 | <p>Converts hyphenated strings to camelCase:</p> | |
448 | <pre class="literal-block"> | |
449 | assert( camelize("border-left") == "borderLeft" ); | |
450 | </pre> | |
451 | <dl class="docutils"> | |
452 | <dt><em>Availability</em>:</dt> | |
453 | <dd>Available in MochiKit 1.4+</dd> | |
454 | </dl> | |
455 | </blockquote> | |
456 | <p> | |
457 | <a name="fn-clone"></a> | |
458 | <a class="mochidef reference" href="#fn-clone">clone(obj)</a>:</p> | |
459 | <blockquote> | |
460 | <p>Return a new object using <tt class="docutils literal"><span class="pre">obj</span></tt> as its prototype. Use this if | |
461 | you want to return a mutable object (e.g. instance state), but | |
462 | don't want the user to mutate it. If they do, it won't have any | |
463 | effect on the original <tt class="docutils literal"><span class="pre">obj</span></tt>.</p> | |
464 | <p>Note that this is a shallow clone, so mutable properties will have | |
465 | to be cloned separately if you want to "protect" them.</p> | |
466 | <dl class="docutils"> | |
467 | <dt><em>Availability</em>:</dt> | |
468 | <dd>Available in MochiKit 1.3.1+</dd> | |
469 | </dl> | |
470 | </blockquote> | |
471 | <p> | |
472 | <a name="fn-compare"></a> | |
473 | <a class="mochidef reference" href="#fn-compare">compare(a, b)</a>:</p> | |
474 | <blockquote> | |
475 | <p>Compare two objects in a sensible manner. Currently this is:</p> | |
476 | <ol class="arabic simple"> | |
477 | <li><tt class="docutils literal"><span class="pre">undefined</span></tt> and <tt class="docutils literal"><span class="pre">null</span></tt> compare equal to each other</li> | |
478 | <li><tt class="docutils literal"><span class="pre">undefined</span></tt> and <tt class="docutils literal"><span class="pre">null</span></tt> are less than anything else</li> | |
479 | <li>If JavaScript says <tt class="docutils literal"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></tt>, then we trust it</li> | |
480 | <li>comparators registered with registerComparator are used to | |
481 | find a good comparator. Built-in comparators are currently | |
482 | available for <tt class="docutils literal"><span class="pre">Array</span></tt>-like and <tt class="docutils literal"><span class="pre">Date</span></tt>-like objects.</li> | |
483 | <li>Otherwise hope that the built-in comparison operators do | |
484 | something useful, which should work for numbers and strings.</li> | |
485 | <li>If neither <tt class="docutils literal"><span class="pre">a</span> <span class="pre"><</span> <span class="pre">b</span></tt> or <tt class="docutils literal"><span class="pre">a</span> <span class="pre">></span> <span class="pre">b</span></tt>, then throw a <tt class="docutils literal"><span class="pre">TypeError</span></tt></li> | |
486 | </ol> | |
487 | <p>Returns what one would expect from a comparison function:</p> | |
488 | <table border="1" class="docutils"> | |
489 | <colgroup> | |
490 | <col width="42%" /> | |
491 | <col width="58%" /> | |
492 | </colgroup> | |
493 | <tbody valign="top"> | |
494 | <tr><td>Value</td> | |
495 | <td>Condition</td> | |
496 | </tr> | |
497 | <tr><td><tt class="docutils literal"><span class="pre">0</span></tt></td> | |
498 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></tt></td> | |
499 | </tr> | |
500 | <tr><td><tt class="docutils literal"><span class="pre">1</span></tt></td> | |
501 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">></span> <span class="pre">b</span></tt></td> | |
502 | </tr> | |
503 | <tr><td><tt class="docutils literal"><span class="pre">-1</span></tt></td> | |
504 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre"><</span> <span class="pre">b</span></tt></td> | |
505 | </tr> | |
506 | </tbody> | |
507 | </table> | |
508 | <dl class="docutils"> | |
509 | <dt><em>Availability</em>:</dt> | |
510 | <dd>Available in MochiKit 1.3.1+</dd> | |
511 | </dl> | |
512 | </blockquote> | |
513 | <p> | |
514 | <a name="fn-compose"></a> | |
515 | <a class="mochidef reference" href="#fn-compose">compose(f1, f2, ..., fN)</a>:</p> | |
516 | <blockquote> | |
517 | <p>Return a new function as the combination of the given function | |
518 | arguments, equivalent to <tt class="docutils literal"><span class="pre">f1(f2(arguments))</span></tt>.</p> | |
519 | <dl class="docutils"> | |
520 | <dt><em>Availability</em>:</dt> | |
521 | <dd>Available in MochiKit 1.4+</dd> | |
522 | </dl> | |
523 | </blockquote> | |
524 | <p> | |
525 | <a name="fn-concat"></a> | |
526 | <a class="mochidef reference" href="#fn-concat">concat(lst[, ...])</a>:</p> | |
527 | <blockquote> | |
528 | <p>Concatenates all given <tt class="docutils literal"><span class="pre">Array</span></tt>-like arguments and returns | |
529 | a new <tt class="docutils literal"><span class="pre">Array</span></tt>:</p> | |
530 | <pre class="literal-block"> | |
531 | var lst = concat(["1","3","5"], ["2","4","6"]); | |
532 | assert( lst.toString() == "1,3,5,2,4,6" ); | |
533 | </pre> | |
534 | <dl class="docutils"> | |
535 | <dt><em>Availability</em>:</dt> | |
536 | <dd>Available in MochiKit 1.3.1+</dd> | |
537 | </dl> | |
538 | </blockquote> | |
539 | <p> | |
540 | <a name="fn-counter"></a> | |
541 | <a class="mochidef reference" href="#fn-counter">counter(n=1)</a>:</p> | |
542 | <blockquote> | |
543 | <p>Returns a function that will return a number one greater than | |
544 | the previous returned value, starting at <tt class="docutils literal"><span class="pre">n</span></tt>. For example:</p> | |
545 | <pre class="literal-block"> | |
546 | nextId = counter() | |
547 | assert( nextId() == 1 ) | |
548 | assert( nextId() == 2 ) | |
549 | </pre> | |
550 | <p>For an iterator with this behavior, see | |
551 | <a class="mochiref reference" href="Iter.html#fn-count">MochiKit.Iter.count</a>.</p> | |
552 | <dl class="docutils"> | |
553 | <dt><em>Availability</em>:</dt> | |
554 | <dd>Available in MochiKit 1.3.1+</dd> | |
555 | </dl> | |
556 | </blockquote> | |
557 | <p> | |
558 | <a name="fn-extend"></a> | |
559 | <a class="mochidef reference" href="#fn-extend">extend(self, obj, skip=0)</a>:</p> | |
560 | <blockquote> | |
561 | <p>Mutate the array <tt class="docutils literal"><span class="pre">self</span></tt> by extending it with an <tt class="docutils literal"><span class="pre">Array</span></tt>-like | |
562 | <tt class="docutils literal"><span class="pre">obj</span></tt>, starting from index <tt class="docutils literal"><span class="pre">skip</span></tt>. If <tt class="docutils literal"><span class="pre">null</span></tt> is given as the | |
563 | initial array, a new one will be created.</p> | |
564 | <p>This mutates <em>and returns</em> <tt class="docutils literal"><span class="pre">self</span></tt>, be warned.</p> | |
565 | <dl class="docutils"> | |
566 | <dt><em>Availability</em>:</dt> | |
567 | <dd>Available in MochiKit 1.3.1+</dd> | |
568 | </dl> | |
569 | </blockquote> | |
570 | <p> | |
571 | <a name="fn-evaljson"></a> | |
572 | <a class="mochidef reference" href="#fn-evaljson">evalJSON(aJSONString)</a>:</p> | |
573 | <blockquote> | |
574 | <p>Unserialize a JSON <a class="footnote-reference" href="#id7" id="id4" name="id4">[1]</a> represenation of an object.</p> | |
575 | <p>Note that this uses the <tt class="docutils literal"><span class="pre">eval</span></tt> function of the interpreter, and | |
576 | therefore trusts the contents of <tt class="docutils literal"><span class="pre">aJSONString</span></tt> to be safe. This | |
577 | is acceptable when the JSON and JavaScript application originate | |
578 | from the same server, but in other scenarios it may not be the | |
579 | appropriate security model. Currently, a validating JSON parser is | |
580 | beyond the scope of MochiKit, but there is one available from | |
581 | json.org <a class="footnote-reference" href="#id7" id="id5" name="id5">[1]</a>.</p> | |
582 | <dl class="docutils"> | |
583 | <dt><em>Availability</em>:</dt> | |
584 | <dd>Available in MochiKit 1.3.1+</dd> | |
585 | </dl> | |
586 | </blockquote> | |
587 | <p> | |
588 | <a name="fn-filter"></a> | |
589 | <a class="mochidef reference" href="#fn-filter">filter(fn, lst)</a>:</p> | |
590 | <blockquote> | |
591 | <p>Returns a new <tt class="docutils literal"><span class="pre">Array</span></tt> composed of all elements from <tt class="docutils literal"><span class="pre">lst</span></tt> | |
592 | where <tt class="docutils literal"><span class="pre">fn(lst[i])</span></tt> returns a true value.</p> | |
593 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, <tt class="docutils literal"><span class="pre">operator.truth</span></tt> will be used.</p> | |
594 | <dl class="docutils"> | |
595 | <dt><em>Availability</em>:</dt> | |
596 | <dd>Available in MochiKit 1.3.1+</dd> | |
597 | </dl> | |
598 | </blockquote> | |
599 | <p> | |
600 | <a name="fn-findvalue"></a> | |
601 | <a class="mochidef reference" href="#fn-findvalue">findValue(lst, value, start=0, end=lst.length)</a>:</p> | |
602 | <blockquote> | |
603 | <p>Finds the index of <tt class="docutils literal"><span class="pre">value</span></tt> in the <tt class="docutils literal"><span class="pre">Array</span></tt>-like object <tt class="docutils literal"><span class="pre">lst</span></tt> | |
604 | using <a class="mochiref reference" href="#fn-compare">compare</a>. The search starts at the index | |
605 | <tt class="docutils literal"><span class="pre">start</span></tt>, and ends at the index <tt class="docutils literal"><span class="pre">end</span> <span class="pre">-</span> <span class="pre">1</span></tt>. If <tt class="docutils literal"><span class="pre">value</span></tt> is not | |
606 | found in <tt class="docutils literal"><span class="pre">lst</span></tt>, it will return <tt class="docutils literal"><span class="pre">-1</span></tt>.</p> | |
607 | <p>For example:</p> | |
608 | <pre class="literal-block"> | |
609 | assert( findValue([1, 2, 3, 2, 1], 2) == 1 ) | |
610 | assert( findValue([1, 2, 3, 2, 1], 2, 2) == 3 ) | |
611 | </pre> | |
612 | <dl class="docutils"> | |
613 | <dt><em>Availability</em>:</dt> | |
614 | <dd>Available in MochiKit 1.3.1+</dd> | |
615 | </dl> | |
616 | </blockquote> | |
617 | <p> | |
618 | <a name="fn-findidentical"></a> | |
619 | <a class="mochidef reference" href="#fn-findidentical">findIdentical(lst, value, start=0, end=lst.length)</a>:</p> | |
620 | <blockquote> | |
621 | <p>Finds the index of <tt class="docutils literal"><span class="pre">value</span></tt> in the <tt class="docutils literal"><span class="pre">Array</span></tt>-like object <tt class="docutils literal"><span class="pre">lst</span></tt> | |
622 | using the <tt class="docutils literal"><span class="pre">===</span></tt> operator. The search starts at the index | |
623 | <tt class="docutils literal"><span class="pre">start</span></tt>, and ends at the index <tt class="docutils literal"><span class="pre">end</span> <span class="pre">-</span> <span class="pre">1</span></tt>. If <tt class="docutils literal"><span class="pre">value</span></tt> is not | |
624 | found in <tt class="docutils literal"><span class="pre">lst</span></tt>, it will return <tt class="docutils literal"><span class="pre">-1</span></tt>.</p> | |
625 | <p>You should use this function instead of <a class="mochiref reference" href="#fn-findvalue">findValue</a> if | |
626 | <tt class="docutils literal"><span class="pre">lst</span></tt> may be comprised of objects for which no comparator is | |
627 | defined and all you care about is finding an identical object | |
628 | (e.g. the same instance), or if <tt class="docutils literal"><span class="pre">lst</span></tt> is comprised of just | |
629 | numbers or strings and performance is important.</p> | |
630 | <p>For example:</p> | |
631 | <pre class="literal-block"> | |
632 | assert( findIdentical([1, 2, 3, 2, 1], 2) == 1 ) | |
633 | assert( findIdentical([1, 2, 3, 2, 1], 2, 2) == 3 ) | |
634 | </pre> | |
635 | <dl class="docutils"> | |
636 | <dt><em>Availability</em>:</dt> | |
637 | <dd>Available in MochiKit 1.3.1+</dd> | |
638 | </dl> | |
639 | </blockquote> | |
640 | <p> | |
641 | <a name="fn-flattenarguments"></a> | |
642 | <a class="mochidef reference" href="#fn-flattenarguments">flattenArguments(arg[, ...])</a>:</p> | |
643 | <blockquote> | |
644 | <p>Given a bunch of arguments, return a single <tt class="docutils literal"><span class="pre">Array</span></tt> containing | |
645 | all of those arguments. Any <tt class="docutils literal"><span class="pre">Array</span></tt>-like argument will be extended | |
646 | in-place, e.g.:</p> | |
647 | <pre class="literal-block"> | |
648 | compare(flattenArguments(1, [2, 3, [4, 5]]), [1, 2, 3, 4, 5]) == 0 | |
649 | </pre> | |
650 | <dl class="docutils"> | |
651 | <dt><em>Availability</em>:</dt> | |
652 | <dd>Available in MochiKit 1.3.1+</dd> | |
653 | </dl> | |
654 | </blockquote> | |
655 | <p> | |
656 | <a name="fn-flattenarray"></a> | |
657 | <a class="mochidef reference" href="#fn-flattenarray">flattenArray(lst)</a>:</p> | |
658 | <blockquote> | |
659 | <p>Return a new <tt class="docutils literal"><span class="pre">Array</span></tt> consisting of every item in lst with <tt class="docutils literal"><span class="pre">Array</span></tt> | |
660 | items expanded in-place recursively. This differs from | |
661 | <a class="mochiref reference" href="#fn-flattenarguments">flattenArguments</a> in that it only takes one argument and | |
662 | it only flattens items that are <tt class="docutils literal"><span class="pre">instanceof</span> <span class="pre">Array</span></tt>.</p> | |
663 | <blockquote> | |
664 | compare(flattenArray([1, [2, 3, [4, 5]]]), [1, 2, 3, 4, 5]) == 0</blockquote> | |
665 | <dl class="docutils"> | |
666 | <dt><em>Availability</em>:</dt> | |
667 | <dd>Available in MochiKit 1.4+</dd> | |
668 | </dl> | |
669 | </blockquote> | |
670 | <p> | |
671 | <a name="fn-forwardcall"></a> | |
672 | <a class="mochidef reference" href="#fn-forwardcall">forwardCall(name)</a>:</p> | |
673 | <blockquote> | |
674 | <p>Returns a function that forwards a method call to | |
675 | <tt class="docutils literal"><span class="pre">this.name(...)</span></tt></p> | |
676 | <dl class="docutils"> | |
677 | <dt><em>Availability</em>:</dt> | |
678 | <dd>Available in MochiKit 1.3.1+</dd> | |
679 | </dl> | |
680 | </blockquote> | |
681 | <p> | |
682 | <a name="fn-isarraylike"></a> | |
683 | <a class="mochidef reference" href="#fn-isarraylike">isArrayLike(obj[, ...])</a>:</p> | |
684 | <blockquote> | |
685 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all given arguments are <tt class="docutils literal"><span class="pre">Array</span></tt>-like (have a | |
686 | <tt class="docutils literal"><span class="pre">.length</span></tt> property and <tt class="docutils literal"><span class="pre">typeof(obj)</span> <span class="pre">==</span> <span class="pre">'object'</span></tt>)</p> | |
687 | <dl class="docutils"> | |
688 | <dt><em>Availability</em>:</dt> | |
689 | <dd>Available in MochiKit 1.3.1+</dd> | |
690 | </dl> | |
691 | </blockquote> | |
692 | <p> | |
693 | <a name="fn-isdatelike"></a> | |
694 | <a class="mochidef reference" href="#fn-isdatelike">isDateLike(obj[, ...])</a>:</p> | |
695 | <blockquote> | |
696 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all given arguments are <tt class="docutils literal"><span class="pre">Date</span></tt>-like (have a | |
697 | <tt class="docutils literal"><span class="pre">.getTime()</span></tt> method)</p> | |
698 | <dl class="docutils"> | |
699 | <dt><em>Availability</em>:</dt> | |
700 | <dd>Available in MochiKit 1.3.1+</dd> | |
701 | </dl> | |
702 | </blockquote> | |
703 | <p> | |
704 | <a name="fn-isempty"></a> | |
705 | <a class="mochidef reference" href="#fn-isempty">isEmpty(obj[, ...])</a>:</p> | |
706 | <blockquote> | |
707 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all the given <tt class="docutils literal"><span class="pre">Array</span></tt>-like or string | |
708 | arguments are empty <tt class="docutils literal"><span class="pre">(obj.length</span> <span class="pre">==</span> <span class="pre">0)</span></tt></p> | |
709 | <dl class="docutils"> | |
710 | <dt><em>Availability</em>:</dt> | |
711 | <dd>Available in MochiKit 1.3.1+</dd> | |
712 | </dl> | |
713 | </blockquote> | |
714 | <p> | |
715 | <a name="fn-isnotempty"></a> | |
716 | <a class="mochidef reference" href="#fn-isnotempty">isNotEmpty(obj[, ...])</a>:</p> | |
717 | <blockquote> | |
718 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all the given <tt class="docutils literal"><span class="pre">Array</span></tt>-like or string | |
719 | arguments are not empty <tt class="docutils literal"><span class="pre">(obj.length</span> <span class="pre">></span> <span class="pre">0)</span></tt></p> | |
720 | <dl class="docutils"> | |
721 | <dt><em>Availability</em>:</dt> | |
722 | <dd>Available in MochiKit 1.3.1+</dd> | |
723 | </dl> | |
724 | </blockquote> | |
725 | <p> | |
726 | <a name="fn-isnull"></a> | |
727 | <a class="mochidef reference" href="#fn-isnull">isNull(obj[, ...])</a>:</p> | |
728 | <blockquote> | |
729 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all arguments are <tt class="docutils literal"><span class="pre">null</span></tt>.</p> | |
730 | <dl class="docutils"> | |
731 | <dt><em>Availability</em>:</dt> | |
732 | <dd>Available in MochiKit 1.3.1+</dd> | |
733 | </dl> | |
734 | </blockquote> | |
735 | <p> | |
736 | <a name="fn-isundefinedornull"></a> | |
737 | <a class="mochidef reference" href="#fn-isundefinedornull">isUndefinedOrNull(obj[, ...])</a>:</p> | |
738 | <blockquote> | |
739 | <p>Returns <tt class="docutils literal"><span class="pre">true</span></tt> if all arguments are undefined or <tt class="docutils literal"><span class="pre">null</span></tt></p> | |
740 | <dl class="docutils"> | |
741 | <dt><em>Availability</em>:</dt> | |
742 | <dd>Available in MochiKit 1.3.1+</dd> | |
743 | </dl> | |
744 | </blockquote> | |
745 | <p> | |
746 | <a name="fn-itemgetter"></a> | |
747 | <a class="mochidef reference" href="#fn-itemgetter">itemgetter(name)</a>:</p> | |
748 | <blockquote> | |
749 | <p>Returns a <tt class="docutils literal"><span class="pre">function(obj)</span></tt> that returns <tt class="docutils literal"><span class="pre">obj[name]</span></tt></p> | |
750 | <dl class="docutils"> | |
751 | <dt><em>Availability</em>:</dt> | |
752 | <dd>Available in MochiKit 1.3.1+</dd> | |
753 | </dl> | |
754 | </blockquote> | |
755 | <p> | |
756 | <a name="fn-items"></a> | |
757 | <a class="mochidef reference" href="#fn-items">items(obj)</a>:</p> | |
758 | <blockquote> | |
759 | <p>Return an <tt class="docutils literal"><span class="pre">Array</span></tt> of <tt class="docutils literal"><span class="pre">[propertyName,</span> <span class="pre">propertyValue]</span></tt> pairs for | |
760 | the given <tt class="docutils literal"><span class="pre">obj</span></tt> (in the order determined by <tt class="docutils literal"><span class="pre">for</span> <span class="pre">propName</span> <span class="pre">in</span> | |
761 | <span class="pre">obj</span></tt>).</p> | |
762 | <dl class="docutils"> | |
763 | <dt><em>Availability</em>:</dt> | |
764 | <dd>Available in MochiKit 1.3.1+</dd> | |
765 | </dl> | |
766 | </blockquote> | |
767 | <p> | |
768 | <a name="fn-keycomparator"></a> | |
769 | <a class="mochidef reference" href="#fn-keycomparator">keyComparator(key[, ...])</a>:</p> | |
770 | <blockquote> | |
771 | <p>A comparator factory that compares <tt class="docutils literal"><span class="pre">a[key]</span></tt> with <tt class="docutils literal"><span class="pre">b[key]</span></tt>. | |
772 | e.g.:</p> | |
773 | <pre class="literal-block"> | |
774 | var lst = ["a", "bbb", "cc"]; | |
775 | lst.sort(keyComparator("length")); | |
776 | assert( lst.toString() == "a,cc,bbb" ); | |
777 | </pre> | |
778 | <dl class="docutils"> | |
779 | <dt><em>Availability</em>:</dt> | |
780 | <dd>Available in MochiKit 1.3.1+</dd> | |
781 | </dl> | |
782 | </blockquote> | |
783 | <p> | |
784 | <a name="fn-keys"></a> | |
785 | <a class="mochidef reference" href="#fn-keys">keys(obj)</a>:</p> | |
786 | <blockquote> | |
787 | <p>Return an <tt class="docutils literal"><span class="pre">Array</span></tt> of the property names of an object (in the | |
788 | order determined by <tt class="docutils literal"><span class="pre">for</span> <span class="pre">propName</span> <span class="pre">in</span> <span class="pre">obj</span></tt>).</p> | |
789 | <dl class="docutils"> | |
790 | <dt><em>Availability</em>:</dt> | |
791 | <dd>Available in MochiKit 1.3.1+</dd> | |
792 | </dl> | |
793 | </blockquote> | |
794 | <p> | |
795 | <a name="fn-listmax"></a> | |
796 | <a class="mochidef reference" href="#fn-listmax">listMax(lst)</a>:</p> | |
797 | <blockquote> | |
798 | <p>Return the largest element of an <tt class="docutils literal"><span class="pre">Array</span></tt>-like object, as | |
799 | determined by <a class="mochiref reference" href="#fn-compare">compare</a>. This is a special form of | |
800 | <a class="mochiref reference" href="#fn-listminmax">listMinMax</a>, specifically | |
801 | <a class="mochiref reference" href="#fn-partial">partial(listMinMax, 1)</a>.</p> | |
802 | <dl class="docutils"> | |
803 | <dt><em>Availability</em>:</dt> | |
804 | <dd>Available in MochiKit 1.3.1+</dd> | |
805 | </dl> | |
806 | </blockquote> | |
807 | <p> | |
808 | <a name="fn-listmin"></a> | |
809 | <a class="mochidef reference" href="#fn-listmin">listMin(lst)</a>:</p> | |
810 | <blockquote> | |
811 | <p>Return the smallest element of an <tt class="docutils literal"><span class="pre">Array</span></tt>-like object, as | |
812 | determined by <a class="mochiref reference" href="#fn-compare">compare</a>. This is a special form of | |
813 | <a class="mochiref reference" href="#fn-listminmax">listMinMax</a>, specifically | |
814 | <a class="mochiref reference" href="#fn-partial">partial(listMinMax, -1)</a>.</p> | |
815 | <dl class="docutils"> | |
816 | <dt><em>Availability</em>:</dt> | |
817 | <dd>Available in MochiKit 1.3.1+</dd> | |
818 | </dl> | |
819 | </blockquote> | |
820 | <p> | |
821 | <a name="fn-listminmax"></a> | |
822 | <a class="mochidef reference" href="#fn-listminmax">listMinMax(which, lst)</a>:</p> | |
823 | <blockquote> | |
824 | <p>If <tt class="docutils literal"><span class="pre">which</span> <span class="pre">==</span> <span class="pre">-1</span></tt> then it will return the smallest element of the | |
825 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like <tt class="docutils literal"><span class="pre">lst</span></tt>. This is also available as | |
826 | <a class="mochiref reference" href="#fn-listmin">listMin(lst)</a>.</p> | |
827 | <p>If <tt class="docutils literal"><span class="pre">which</span> <span class="pre">==</span> <span class="pre">1</span></tt> then it will return the largest element of the | |
828 | <tt class="docutils literal"><span class="pre">Array</span></tt>-like <tt class="docutils literal"><span class="pre">lst</span></tt>. This is also available as | |
829 | <a class="mochiref reference" href="#fn-listmax">listMax(list)</a>.</p> | |
830 | <dl class="docutils"> | |
831 | <dt><em>Availability</em>:</dt> | |
832 | <dd>Available in MochiKit 1.3.1+</dd> | |
833 | </dl> | |
834 | </blockquote> | |
835 | <p> | |
836 | <a name="fn-map"></a> | |
837 | <a class="mochidef reference" href="#fn-map">map(fn, lst[, ...])</a>:</p> | |
838 | <blockquote> | |
839 | <p>Return a new array composed of the results of <tt class="docutils literal"><span class="pre">fn(x)</span></tt> for every | |
840 | <tt class="docutils literal"><span class="pre">x</span></tt> in <tt class="docutils literal"><span class="pre">lst</span></tt>.</p> | |
841 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, and only one sequence argument is given the | |
842 | identity function is used.</p> | |
843 | <blockquote> | |
844 | <a class="mochiref reference" href="#fn-map">map(null, lst)</a> -> <tt class="docutils literal"><span class="pre">lst.slice()</span></tt>;</blockquote> | |
845 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is not <tt class="docutils literal"><span class="pre">null</span></tt> and more than one sequence argument is | |
846 | given, then one element from each sequence is used to build the | |
847 | argument list for <tt class="docutils literal"><span class="pre">fn</span></tt>.</p> | |
848 | <blockquote> | |
849 | <dl class="docutils"> | |
850 | <dt><a class="mochiref reference" href="#fn-map">map(fn, p, q, ...)</a></dt> | |
851 | <dd>-> <tt class="docutils literal"><span class="pre">[fn(p[0],</span> <span class="pre">q[0],</span> <span class="pre">..),</span> <span class="pre">fn(p[1],</span> <span class="pre">q[1],</span> <span class="pre">...),</span> <span class="pre">...]</span></tt></dd> | |
852 | </dl> | |
853 | </blockquote> | |
854 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, and more than one sequence is given as | |
855 | arguments, then the <tt class="docutils literal"><span class="pre">Array</span></tt> function is used, making it | |
856 | equivalent to <a class="mochiref reference" href="Iter.html#fn-zip">MochiKit.Iter.zip</a>.</p> | |
857 | <blockquote> | |
858 | <dl class="docutils"> | |
859 | <dt><a class="mochiref reference" href="#fn-map">map(null, p, q, ...)</a></dt> | |
860 | <dd>-> <a class="mochiref reference" href="Iter.html#fn-zip">MochiKit.Iter.zip(p, q, ...)</a> | |
861 | -> <tt class="docutils literal"><span class="pre">[[p0,</span> <span class="pre">q0,</span> <span class="pre">...],</span> <span class="pre">[p1,</span> <span class="pre">q1,</span> <span class="pre">...],</span> <span class="pre">...];</span></tt></dd> | |
862 | </dl> | |
863 | </blockquote> | |
864 | <dl class="docutils"> | |
865 | <dt><em>Availability</em>:</dt> | |
866 | <dd>Available in MochiKit 1.3.1+</dd> | |
867 | </dl> | |
868 | </blockquote> | |
869 | <p> | |
870 | <a name="fn-mean"></a> | |
871 | <a class="mochidef reference" href="#fn-mean">mean(lst[, ...])</a>:</p> | |
872 | <blockquote> | |
873 | <p>Returns the arithmetic mean (average) of the argument list, or an array. | |
874 | This function applies <a class="mochiref reference" href="#fn-flattenarguments">flattenArguments()</a> to the argument list.</p> | |
875 | <dl class="docutils"> | |
876 | <dt><em>Availability</em>:</dt> | |
877 | <dd>Available in MochiKit 1.4+</dd> | |
878 | </dl> | |
879 | </blockquote> | |
880 | <p> | |
881 | <a name="fn-median"></a> | |
882 | <a class="mochidef reference" href="#fn-median">median(lst[, ...])</a>:</p> | |
883 | <blockquote> | |
884 | <p>Returns the median of the argument list, or an array. This function | |
885 | applies <a class="mochiref reference" href="#fn-flattenarguments">flattenArguments()</a> to the argument list.</p> | |
886 | <dl class="docutils"> | |
887 | <dt><em>Availability</em>:</dt> | |
888 | <dd>Available in MochiKit 1.4+</dd> | |
889 | </dl> | |
890 | </blockquote> | |
891 | <p> | |
892 | <a name="fn-merge"></a> | |
893 | <a class="mochidef reference" href="#fn-merge">merge(obj[, ...])</a>:</p> | |
894 | <blockquote> | |
895 | <p>Create a new instance of <tt class="docutils literal"><span class="pre">Object</span></tt> that contains every property | |
896 | from all given objects. If a property is defined on more than one | |
897 | of the objects, the last property is used.</p> | |
898 | <p>This is a special form of <a class="mochiref reference" href="#fn-update">update(self, obj[, ...])</a>, | |
899 | specifically, it is defined as <a class="mochiref reference" href="#fn-partial">partial(update, null)</a>.</p> | |
900 | <dl class="docutils"> | |
901 | <dt><em>Availability</em>:</dt> | |
902 | <dd>Available in MochiKit 1.3.1+</dd> | |
903 | </dl> | |
904 | </blockquote> | |
905 | <p> | |
906 | <a name="fn-method"></a> | |
907 | <a class="mochidef reference" href="#fn-method">method(self, func, ...)</a>:</p> | |
908 | <blockquote> | |
909 | <p>Alternate form of <a class="mochiref reference" href="#fn-bind">bind</a> that takes the object before | |
910 | the function. These two calls are equivalent:</p> | |
911 | <pre class="literal-block"> | |
912 | bind("method", myobject) | |
913 | method(myobject, "method") | |
914 | </pre> | |
915 | <dl class="docutils"> | |
916 | <dt><em>Availability</em>:</dt> | |
917 | <dd>Available in MochiKit 1.3.1+</dd> | |
918 | </dl> | |
919 | </blockquote> | |
920 | <p> | |
921 | <a name="fn-methodcaller"></a> | |
922 | <a class="mochidef reference" href="#fn-methodcaller">methodcaller(name[, args...])</a>:</p> | |
923 | <blockquote> | |
924 | <p>Return a new function that calls a method on its argument, | |
925 | for example:</p> | |
926 | <pre class="literal-block"> | |
927 | lst = map(methodcaller("toLowerCase"), ["THIS", "is", "LoWeRCaSe"]); | |
928 | assert( lst.join(" ") == "this is lowercase" ); | |
929 | </pre> | |
930 | <dl class="docutils"> | |
931 | <dt><em>Availability</em>:</dt> | |
932 | <dd>Available in MochiKit 1.4+</dd> | |
933 | </dl> | |
934 | </blockquote> | |
935 | <p> | |
936 | <a name="fn-namefunctions"></a> | |
937 | <a class="mochidef reference" href="#fn-namefunctions">nameFunctions(namespace)</a>:</p> | |
938 | <blockquote> | |
939 | <p>Given a <tt class="docutils literal"><span class="pre">namespace</span></tt> (object or function) with a <tt class="docutils literal"><span class="pre">NAME</span></tt> | |
940 | property, find all methods in it and give them nice <tt class="docutils literal"><span class="pre">NAME</span></tt> | |
941 | properties too (for use with <a class="mochiref reference" href="#fn-repr">repr</a>). e.g.:</p> | |
942 | <pre class="literal-block"> | |
943 | namespace = { | |
944 | NAME: "Awesome", | |
945 | Dude: function () {} | |
946 | } | |
947 | nameFunctions(namespace); | |
948 | assert( namespace.Dude.NAME == 'Awesome.Dude' ); | |
949 | </pre> | |
950 | <dl class="docutils"> | |
951 | <dt><em>Availability</em>:</dt> | |
952 | <dd>Available in MochiKit 1.3.1+</dd> | |
953 | </dl> | |
954 | </blockquote> | |
955 | <p> | |
956 | <a name="fn-noop"></a> | |
957 | <a class="mochidef reference" href="#fn-noop">noop()</a>:</p> | |
958 | <blockquote> | |
959 | <p>A function that performs no operation. Use this where you would | |
960 | otherwise use <tt class="docutils literal"><span class="pre">(function</span> <span class="pre">()</span> <span class="pre">{})</span></tt> in order to avoid Internet | |
961 | Explorer cyclic garbage leakage.</p> | |
962 | <dl class="docutils"> | |
963 | <dt><em>Availability</em>:</dt> | |
964 | <dd>Available in MochiKit 1.4</dd> | |
965 | </dl> | |
966 | </blockquote> | |
967 | <p> | |
968 | <a name="fn-objequal"></a> | |
969 | <a class="mochidef reference" href="#fn-objequal">objEqual(a, b)</a>:</p> | |
970 | <blockquote> | |
971 | <p>Return <tt class="docutils literal"><span class="pre">true</span></tt> if <tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">==</span> <span class="pre">0</span></tt></p> | |
972 | <dl class="docutils"> | |
973 | <dt><em>Availability</em>:</dt> | |
974 | <dd>Available in MochiKit 1.3.1+</dd> | |
975 | </dl> | |
976 | </blockquote> | |
977 | <p> | |
978 | <a name="fn-nodewalk"></a> | |
979 | <a class="mochidef reference" href="#fn-nodewalk">nodeWalk(node, visitor)</a>:</p> | |
980 | <blockquote> | |
981 | <p>Non-recursive generic node walking function (e.g. for a DOM).</p> | |
982 | <p>The walk order for nodeWalk is breadth first, meaning that all | |
983 | siblings will be visited before any children.</p> | |
984 | <dl class="docutils"> | |
985 | <dt><tt class="docutils literal"><span class="pre">node</span></tt>:</dt> | |
986 | <dd>The initial node to be searched.</dd> | |
987 | <dt><tt class="docutils literal"><span class="pre">visitor</span></tt>:</dt> | |
988 | <dd>The visitor function, will be called as <tt class="docutils literal"><span class="pre">visitor(node)</span></tt>, and | |
989 | should return an <tt class="docutils literal"><span class="pre">Array</span></tt>-like of nodes to be searched next | |
990 | (e.g. <tt class="docutils literal"><span class="pre">node.childNodes</span></tt>). Leaf nodes may return <tt class="docutils literal"><span class="pre">null</span></tt> or | |
991 | <tt class="docutils literal"><span class="pre">undefined</span></tt>.</dd> | |
992 | <dt><em>Availability</em>:</dt> | |
993 | <dd>Available in MochiKit 1.3.1+</dd> | |
994 | </dl> | |
995 | </blockquote> | |
996 | <p> | |
997 | <a name="fn-objmax"></a> | |
998 | <a class="mochidef reference" href="#fn-objmax">objMax(obj[, ...])</a>:</p> | |
999 | <blockquote> | |
1000 | <p>Return the maximum object according to <a class="mochiref reference" href="#fn-compare">compare</a> out of | |
1001 | the given arguments. This is similar to <a class="mochiref reference" href="#fn-listmax">listMax</a>, | |
1002 | except is uses the arguments instead of a given <tt class="docutils literal"><span class="pre">Array</span></tt>-like.</p> | |
1003 | <dl class="docutils"> | |
1004 | <dt><em>Availability</em>:</dt> | |
1005 | <dd>Available in MochiKit 1.3.1+</dd> | |
1006 | </dl> | |
1007 | </blockquote> | |
1008 | <p> | |
1009 | <a name="fn-objmin"></a> | |
1010 | <a class="mochidef reference" href="#fn-objmin">objMin(obj[, ...])</a>:</p> | |
1011 | <blockquote> | |
1012 | <p>Return the minimum object according to <a class="mochiref reference" href="#fn-compare">compare</a> out of | |
1013 | the given arguments. This is similar to <a class="mochiref reference" href="#fn-listmin">listMin</a>, | |
1014 | except it uses the arguments instead of a given <tt class="docutils literal"><span class="pre">Array</span></tt>-like.</p> | |
1015 | <dl class="docutils"> | |
1016 | <dt><em>Availability</em>:</dt> | |
1017 | <dd>Available in MochiKit 1.3.1+</dd> | |
1018 | </dl> | |
1019 | </blockquote> | |
1020 | <p> | |
1021 | <a name="fn-operator"></a> | |
1022 | <a class="mochidef reference" href="#fn-operator">operator</a>:</p> | |
1023 | <blockquote> | |
1024 | <p>A table of JavaScript's operators for usage with <a class="mochiref reference" href="#fn-map">map</a>, | |
1025 | <a class="mochiref reference" href="#fn-filter">filter</a>, etc.</p> | |
1026 | <p>Unary Logic Operators:</p> | |
1027 | <blockquote> | |
1028 | <table border="1" class="docutils"> | |
1029 | <colgroup> | |
1030 | <col width="31%" /> | |
1031 | <col width="40%" /> | |
1032 | <col width="29%" /> | |
1033 | </colgroup> | |
1034 | <thead valign="bottom"> | |
1035 | <tr><th class="head">Operator</th> | |
1036 | <th class="head">Implementation</th> | |
1037 | <th class="head">Description</th> | |
1038 | </tr> | |
1039 | </thead> | |
1040 | <tbody valign="top"> | |
1041 | <tr><td><tt class="docutils literal"><span class="pre">truth(a)</span></tt></td> | |
1042 | <td><tt class="docutils literal"><span class="pre">!!a</span></tt></td> | |
1043 | <td>Logical truth</td> | |
1044 | </tr> | |
1045 | <tr><td><tt class="docutils literal"><span class="pre">lognot(a)</span></tt></td> | |
1046 | <td><tt class="docutils literal"><span class="pre">!a</span></tt></td> | |
1047 | <td>Logical not</td> | |
1048 | </tr> | |
1049 | <tr><td><tt class="docutils literal"><span class="pre">identity(a)</span></tt></td> | |
1050 | <td><tt class="docutils literal"><span class="pre">a</span></tt></td> | |
1051 | <td>Logical identity</td> | |
1052 | </tr> | |
1053 | </tbody> | |
1054 | </table> | |
1055 | </blockquote> | |
1056 | <p>Unary Math Operators:</p> | |
1057 | <blockquote> | |
1058 | <table border="1" class="docutils"> | |
1059 | <colgroup> | |
1060 | <col width="33%" /> | |
1061 | <col width="43%" /> | |
1062 | <col width="25%" /> | |
1063 | </colgroup> | |
1064 | <thead valign="bottom"> | |
1065 | <tr><th class="head">Operator</th> | |
1066 | <th class="head">Implementation</th> | |
1067 | <th class="head">Description</th> | |
1068 | </tr> | |
1069 | </thead> | |
1070 | <tbody valign="top"> | |
1071 | <tr><td><tt class="docutils literal"><span class="pre">not(a)</span></tt></td> | |
1072 | <td><tt class="docutils literal"><span class="pre">~a</span></tt></td> | |
1073 | <td>Bitwise not</td> | |
1074 | </tr> | |
1075 | <tr><td><tt class="docutils literal"><span class="pre">neg(a)</span></tt></td> | |
1076 | <td><tt class="docutils literal"><span class="pre">-a</span></tt></td> | |
1077 | <td>Negation</td> | |
1078 | </tr> | |
1079 | </tbody> | |
1080 | </table> | |
1081 | </blockquote> | |
1082 | <p>Binary Operators:</p> | |
1083 | <blockquote> | |
1084 | <table border="1" class="docutils"> | |
1085 | <colgroup> | |
1086 | <col width="28%" /> | |
1087 | <col width="28%" /> | |
1088 | <col width="45%" /> | |
1089 | </colgroup> | |
1090 | <thead valign="bottom"> | |
1091 | <tr><th class="head">Operator</th> | |
1092 | <th class="head">Implementation</th> | |
1093 | <th class="head">Description</th> | |
1094 | </tr> | |
1095 | </thead> | |
1096 | <tbody valign="top"> | |
1097 | <tr><td><tt class="docutils literal"><span class="pre">add(a,</span> <span class="pre">b)</span></tt></td> | |
1098 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">b</span></tt></td> | |
1099 | <td>Addition</td> | |
1100 | </tr> | |
1101 | <tr><td><tt class="docutils literal"><span class="pre">sub(a,</span> <span class="pre">b)</span></tt></td> | |
1102 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">-</span> <span class="pre">b</span></tt></td> | |
1103 | <td>Subtraction</td> | |
1104 | </tr> | |
1105 | <tr><td><tt class="docutils literal"><span class="pre">div(a,</span> <span class="pre">b)</span></tt></td> | |
1106 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">/</span> <span class="pre">b</span></tt></td> | |
1107 | <td>Division</td> | |
1108 | </tr> | |
1109 | <tr><td><tt class="docutils literal"><span class="pre">mod(a,</span> <span class="pre">b)</span></tt></td> | |
1110 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">%</span> <span class="pre">b</span></tt></td> | |
1111 | <td>Modulus</td> | |
1112 | </tr> | |
1113 | <tr><td><tt class="docutils literal"><span class="pre">mul(a,</span> <span class="pre">b)</span></tt></td> | |
1114 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">*</span> <span class="pre">b</span></tt></td> | |
1115 | <td>Multiplication</td> | |
1116 | </tr> | |
1117 | <tr><td><tt class="docutils literal"><span class="pre">and(a,</span> <span class="pre">b)</span></tt></td> | |
1118 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">&</span> <span class="pre">b</span></tt></td> | |
1119 | <td>Bitwise and</td> | |
1120 | </tr> | |
1121 | <tr><td><tt class="docutils literal"><span class="pre">or(a,</span> <span class="pre">b)</span></tt></td> | |
1122 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">|</span> <span class="pre">b</span></tt></td> | |
1123 | <td>Bitwise or</td> | |
1124 | </tr> | |
1125 | <tr><td><tt class="docutils literal"><span class="pre">xor(a,</span> <span class="pre">b)</span></tt></td> | |
1126 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">^</span> <span class="pre">b</span></tt></td> | |
1127 | <td>Bitwise exclusive or</td> | |
1128 | </tr> | |
1129 | <tr><td><tt class="docutils literal"><span class="pre">lshift(a,</span> <span class="pre">b)</span></tt></td> | |
1130 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre"><<</span> <span class="pre">b</span></tt></td> | |
1131 | <td>Bitwise left shift</td> | |
1132 | </tr> | |
1133 | <tr><td><tt class="docutils literal"><span class="pre">rshift(a,</span> <span class="pre">b)</span></tt></td> | |
1134 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">>></span> <span class="pre">b</span></tt></td> | |
1135 | <td>Bitwise signed right shift</td> | |
1136 | </tr> | |
1137 | <tr><td><tt class="docutils literal"><span class="pre">zrshift(a,</span> <span class="pre">b)</span></tt></td> | |
1138 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">>>></span> <span class="pre">b</span></tt></td> | |
1139 | <td>Bitwise unsigned right shift</td> | |
1140 | </tr> | |
1141 | </tbody> | |
1142 | </table> | |
1143 | </blockquote> | |
1144 | <p>Built-in Comparators:</p> | |
1145 | <blockquote> | |
1146 | <table border="1" class="docutils"> | |
1147 | <colgroup> | |
1148 | <col width="25%" /> | |
1149 | <col width="31%" /> | |
1150 | <col width="44%" /> | |
1151 | </colgroup> | |
1152 | <thead valign="bottom"> | |
1153 | <tr><th class="head">Operator</th> | |
1154 | <th class="head">Implementation</th> | |
1155 | <th class="head">Description</th> | |
1156 | </tr> | |
1157 | </thead> | |
1158 | <tbody valign="top"> | |
1159 | <tr><td><tt class="docutils literal"><span class="pre">eq(a,</span> <span class="pre">b)</span></tt></td> | |
1160 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></tt></td> | |
1161 | <td>Equals</td> | |
1162 | </tr> | |
1163 | <tr><td><tt class="docutils literal"><span class="pre">ne(a,</span> <span class="pre">b)</span></tt></td> | |
1164 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">!=</span> <span class="pre">b</span></tt></td> | |
1165 | <td>Not equals</td> | |
1166 | </tr> | |
1167 | <tr><td><tt class="docutils literal"><span class="pre">gt(a,</span> <span class="pre">b)</span></tt></td> | |
1168 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">></span> <span class="pre">b</span></tt></td> | |
1169 | <td>Greater than</td> | |
1170 | </tr> | |
1171 | <tr><td><tt class="docutils literal"><span class="pre">ge(a,</span> <span class="pre">b)</span></tt></td> | |
1172 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">>=</span> <span class="pre">b</span></tt></td> | |
1173 | <td>Greater than or equal to</td> | |
1174 | </tr> | |
1175 | <tr><td><tt class="docutils literal"><span class="pre">lt(a,</span> <span class="pre">b)</span></tt></td> | |
1176 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre"><</span> <span class="pre">b</span></tt></td> | |
1177 | <td>Less than</td> | |
1178 | </tr> | |
1179 | <tr><td><tt class="docutils literal"><span class="pre">le(a,</span> <span class="pre">b)</span></tt></td> | |
1180 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre"><=</span> <span class="pre">b</span></tt></td> | |
1181 | <td>Less than or equal to</td> | |
1182 | </tr> | |
1183 | </tbody> | |
1184 | </table> | |
1185 | </blockquote> | |
1186 | <p>Strict Built-in Comparators:</p> | |
1187 | <blockquote> | |
1188 | <table border="1" class="docutils"> | |
1189 | <colgroup> | |
1190 | <col width="25%" /> | |
1191 | <col width="31%" /> | |
1192 | <col width="44%" /> | |
1193 | </colgroup> | |
1194 | <thead valign="bottom"> | |
1195 | <tr><th class="head">Operator</th> | |
1196 | <th class="head">Implementation</th> | |
1197 | <th class="head">Description</th> | |
1198 | </tr> | |
1199 | </thead> | |
1200 | <tbody valign="top"> | |
1201 | <tr><td><tt class="docutils literal"><span class="pre">seq(a,</span> <span class="pre">b)</span></tt></td> | |
1202 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">===</span> <span class="pre">b</span></tt></td> | |
1203 | <td>Strict equals</td> | |
1204 | </tr> | |
1205 | <tr><td><tt class="docutils literal"><span class="pre">sne(a,</span> <span class="pre">b)</span></tt></td> | |
1206 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">!==</span> <span class="pre">b</span></tt></td> | |
1207 | <td>Strict not equals</td> | |
1208 | </tr> | |
1209 | </tbody> | |
1210 | </table> | |
1211 | </blockquote> | |
1212 | <p>Extended Comparators (uses <a class="mochiref reference" href="#fn-compare">compare</a>):</p> | |
1213 | <blockquote> | |
1214 | <table border="1" class="docutils"> | |
1215 | <colgroup> | |
1216 | <col width="22%" /> | |
1217 | <col width="39%" /> | |
1218 | <col width="39%" /> | |
1219 | </colgroup> | |
1220 | <thead valign="bottom"> | |
1221 | <tr><th class="head">Operator</th> | |
1222 | <th class="head">Implementation</th> | |
1223 | <th class="head">Description</th> | |
1224 | </tr> | |
1225 | </thead> | |
1226 | <tbody valign="top"> | |
1227 | <tr><td><tt class="docutils literal"><span class="pre">ceq(a,</span> <span class="pre">b)</span></tt></td> | |
1228 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">==</span> <span class="pre">0</span></tt></td> | |
1229 | <td>Equals</td> | |
1230 | </tr> | |
1231 | <tr><td><tt class="docutils literal"><span class="pre">cne(a,</span> <span class="pre">b)</span></tt></td> | |
1232 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">!=</span> <span class="pre">0</span></tt></td> | |
1233 | <td>Not equals</td> | |
1234 | </tr> | |
1235 | <tr><td><tt class="docutils literal"><span class="pre">cgt(a,</span> <span class="pre">b)</span></tt></td> | |
1236 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">==</span> <span class="pre">1</span></tt></td> | |
1237 | <td>Greater than</td> | |
1238 | </tr> | |
1239 | <tr><td><tt class="docutils literal"><span class="pre">cge(a,</span> <span class="pre">b)</span></tt></td> | |
1240 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">!=</span> <span class="pre">-1</span></tt></td> | |
1241 | <td>Greater than or equal to</td> | |
1242 | </tr> | |
1243 | <tr><td><tt class="docutils literal"><span class="pre">clt(a,</span> <span class="pre">b)</span></tt></td> | |
1244 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">==</span> <span class="pre">-1</span></tt></td> | |
1245 | <td>Less than</td> | |
1246 | </tr> | |
1247 | <tr><td><tt class="docutils literal"><span class="pre">cle(a,</span> <span class="pre">b)</span></tt></td> | |
1248 | <td><tt class="docutils literal"><span class="pre">compare(a,</span> <span class="pre">b)</span> <span class="pre">!=</span> <span class="pre">1</span></tt></td> | |
1249 | <td>Less than or equal to</td> | |
1250 | </tr> | |
1251 | </tbody> | |
1252 | </table> | |
1253 | </blockquote> | |
1254 | <p>Binary Logical Operators:</p> | |
1255 | <blockquote> | |
1256 | <table border="1" class="docutils"> | |
1257 | <colgroup> | |
1258 | <col width="33%" /> | |
1259 | <col width="28%" /> | |
1260 | <col width="39%" /> | |
1261 | </colgroup> | |
1262 | <thead valign="bottom"> | |
1263 | <tr><th class="head">Operator</th> | |
1264 | <th class="head">Implementation</th> | |
1265 | <th class="head">Description</th> | |
1266 | </tr> | |
1267 | </thead> | |
1268 | <tbody valign="top"> | |
1269 | <tr><td><tt class="docutils literal"><span class="pre">logand(a,</span> <span class="pre">b)</span></tt></td> | |
1270 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">&&</span> <span class="pre">b</span></tt></td> | |
1271 | <td>Logical and</td> | |
1272 | </tr> | |
1273 | <tr><td><tt class="docutils literal"><span class="pre">logor(a,</span> <span class="pre">b)</span></tt></td> | |
1274 | <td><tt class="docutils literal"><span class="pre">a</span> <span class="pre">||</span> <span class="pre">b</span></tt></td> | |
1275 | <td>Logical or</td> | |
1276 | </tr> | |
1277 | <tr><td><tt class="docutils literal"><span class="pre">contains(a,</span> <span class="pre">b)</span></tt></td> | |
1278 | <td><tt class="docutils literal"><span class="pre">b</span> <span class="pre">in</span> <span class="pre">a</span></tt></td> | |
1279 | <td>Has property (note order)</td> | |
1280 | </tr> | |
1281 | </tbody> | |
1282 | </table> | |
1283 | </blockquote> | |
1284 | <dl class="docutils"> | |
1285 | <dt><em>Availability</em>:</dt> | |
1286 | <dd>Available in MochiKit 1.3.1+</dd> | |
1287 | </dl> | |
1288 | </blockquote> | |
1289 | <p> | |
1290 | <a name="fn-parsequerystring"></a> | |
1291 | <a class="mochidef reference" href="#fn-parsequerystring">parseQueryString(encodedString[, useArrays=false])</a>:</p> | |
1292 | <blockquote> | |
1293 | <p>Parse a name=value pair URL query string into an object with a | |
1294 | property for each pair. e.g.:</p> | |
1295 | <pre class="literal-block"> | |
1296 | var args = parseQueryString("foo=value%20one&bar=two"); | |
1297 | assert( args.foo == "value one" && args.bar == "two" ); | |
1298 | </pre> | |
1299 | <p>If you expect that the query string will reuse the same name, then | |
1300 | give <tt class="docutils literal"><span class="pre">true</span></tt> as a second argument, which will use arrays to store | |
1301 | the values. e.g.:</p> | |
1302 | <pre class="literal-block"> | |
1303 | var args = parseQueryString("foo=one&foo=two", true); | |
1304 | assert( args.foo[0] == "one" && args.foo[1] == "two" ); | |
1305 | </pre> | |
1306 | <dl class="docutils"> | |
1307 | <dt><em>Availability</em>:</dt> | |
1308 | <dd>Available in MochiKit 1.3.1+</dd> | |
1309 | </dl> | |
1310 | </blockquote> | |
1311 | <p> | |
1312 | <a name="fn-partial"></a> | |
1313 | <a class="mochidef reference" href="#fn-partial">partial(func, arg[, ...])</a>:</p> | |
1314 | <blockquote> | |
1315 | <p>Return a partially applied function, e.g.:</p> | |
1316 | <pre class="literal-block"> | |
1317 | addNumbers = function (a, b) { | |
1318 | return a + b; | |
1319 | } | |
1320 | ||
1321 | addOne = partial(addNumbers, 1); | |
1322 | ||
1323 | assert(addOne(2) == 3); | |
1324 | </pre> | |
1325 | <p><a class="mochiref reference" href="#fn-partial">partial</a> is a special form of <a class="mochiref reference" href="#fn-bind">bind</a> that | |
1326 | does not alter the bound <tt class="docutils literal"><span class="pre">self</span></tt> (if any). It is equivalent to | |
1327 | calling:</p> | |
1328 | <pre class="literal-block"> | |
1329 | bind(func, undefined, arg[, ...]); | |
1330 | </pre> | |
1331 | <p>See the documentation for <a class="mochiref reference" href="#fn-bind">bind</a> for more details about | |
1332 | this facility.</p> | |
1333 | <p>This could be used to implement, but is NOT currying.</p> | |
1334 | <dl class="docutils"> | |
1335 | <dt><em>Availability</em>:</dt> | |
1336 | <dd>Available in MochiKit 1.3.1+</dd> | |
1337 | </dl> | |
1338 | </blockquote> | |
1339 | <p> | |
1340 | <a name="fn-querystring"></a> | |
1341 | <a class="mochidef reference" href="#fn-querystring">queryString(names, values)</a>:</p> | |
1342 | <blockquote> | |
1343 | <p>Creates a URL query string from a pair of <tt class="docutils literal"><span class="pre">Array</span></tt>-like objects | |
1344 | representing <tt class="docutils literal"><span class="pre">names</span></tt> and <tt class="docutils literal"><span class="pre">values</span></tt>. Each name=value pair will | |
1345 | be URL encoded by <a class="mochiref reference" href="#fn-urlencode">urlEncode</a>. name=value pairs with a | |
1346 | value of <tt class="docutils literal"><span class="pre">undefined</span></tt> or <tt class="docutils literal"><span class="pre">null</span></tt> will be skipped. e.g.:</p> | |
1347 | <pre class="literal-block"> | |
1348 | var keys = ["foo", "bar"]; | |
1349 | var values = ["value one", "two"]; | |
1350 | assert( queryString(keys, values) == "foo=value%20one&bar=two" ); | |
1351 | </pre> | |
1352 | <dl class="docutils"> | |
1353 | <dt>Alternate form 1:</dt> | |
1354 | <dd><a class="mochiref reference" href="#fn-querystring">queryString(domElement)</a></dd> | |
1355 | </dl> | |
1356 | <p>If <a class="mochiref reference" href="DOM.html">MochiKit.DOM</a> is loaded, one argument is given, and | |
1357 | that argument is either a string or has a <tt class="docutils literal"><span class="pre">nodeType</span></tt> property | |
1358 | greater than zero, then <tt class="docutils literal"><span class="pre">names</span></tt> and <tt class="docutils literal"><span class="pre">values</span></tt> will be the | |
1359 | result of <a class="mochiref reference" href="DOM.html#fn-formcontents">MochiKit.DOM.formContents(domElement)</a>.</p> | |
1360 | <dl class="docutils"> | |
1361 | <dt>Alternate form 2:</dt> | |
1362 | <dd><a class="mochiref reference" href="#fn-querystring">queryString({name: value, ...})</a></dd> | |
1363 | </dl> | |
1364 | <p>Note that when using the alternate form, the order of the | |
1365 | name=value pairs in the resultant query string is dependent on how | |
1366 | the particular JavaScript implementation handles <tt class="docutils literal"><span class="pre">for</span> <span class="pre">(..in..)</span></tt> | |
1367 | property enumeration.</p> | |
1368 | <p>When using the second alternate form, name=value pairs with | |
1369 | <tt class="docutils literal"><span class="pre">typeof(value)</span> <span class="pre">==</span> <span class="pre">"function"</span></tt> are ignored. This is a workaround | |
1370 | for the case where a poorly designed library has modified | |
1371 | <tt class="docutils literal"><span class="pre">Object.prototype</span></tt> and inserted "convenience functions".</p> | |
1372 | <p>Values that are Array-like will be expanded as if they were multiply | |
1373 | defined HTML elements. For example:</p> | |
1374 | <pre class="literal-block"> | |
1375 | assert( queryString({a: [1,2]}) === "a=1&a=2" ); | |
1376 | </pre> | |
1377 | <dl class="docutils"> | |
1378 | <dt>Alternate form 2 (MochiKit 1.4+):</dt> | |
1379 | <dd><a class="mochiref reference" href="#fn-querystring">queryString([names, values])</a></dd> | |
1380 | </dl> | |
1381 | <p>This form behaves identically to <a class="mochiref reference" href="#fn-querystring">queryString(names, values)</a>, | |
1382 | except it takes both arguments as a single Array. This mirrors the | |
1383 | return value of <a class="mochiref reference" href="DOM.html#fn-formcontents">MochiKit.DOM.formContents</a>.</p> | |
1384 | <dl class="docutils"> | |
1385 | <dt><em>Availability</em>:</dt> | |
1386 | <dd>Available in MochiKit 1.3.1+</dd> | |
1387 | </dl> | |
1388 | </blockquote> | |
1389 | <p> | |
1390 | <a name="fn-registercomparator"></a> | |
1391 | <a class="mochidef reference" href="#fn-registercomparator">registerComparator(name, check, comparator[, override])</a>:</p> | |
1392 | <blockquote> | |
1393 | <p>Register a comparator for use with <a class="mochiref reference" href="#fn-compare">compare</a>.</p> | |
1394 | <dl class="docutils"> | |
1395 | <dt><tt class="docutils literal"><span class="pre">name</span></tt>:</dt> | |
1396 | <dd>unique identifier describing the comparator.</dd> | |
1397 | <dt><tt class="docutils literal"><span class="pre">check</span></tt>:</dt> | |
1398 | <dd><tt class="docutils literal"><span class="pre">function(a,</span> <span class="pre">b)</span></tt> that returns <tt class="docutils literal"><span class="pre">true</span></tt> if <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> | |
1399 | can be compared with <tt class="docutils literal"><span class="pre">comparator</span></tt>.</dd> | |
1400 | <dt><tt class="docutils literal"><span class="pre">comparator</span></tt>:</dt> | |
1401 | <dd><p class="first"><tt class="docutils literal"><span class="pre">function(a,</span> <span class="pre">b)</span></tt> that returns:</p> | |
1402 | <table border="1" class="docutils"> | |
1403 | <colgroup> | |
1404 | <col width="39%" /> | |
1405 | <col width="61%" /> | |
1406 | </colgroup> | |
1407 | <tbody valign="top"> | |
1408 | <tr><td>Value</td> | |
1409 | <td>Condition</td> | |
1410 | </tr> | |
1411 | <tr><td>0</td> | |
1412 | <td>a == b</td> | |
1413 | </tr> | |
1414 | <tr><td>1</td> | |
1415 | <td>a > b</td> | |
1416 | </tr> | |
1417 | <tr><td>-1</td> | |
1418 | <td>a < b</td> | |
1419 | </tr> | |
1420 | </tbody> | |
1421 | </table> | |
1422 | <p class="last"><tt class="docutils literal"><span class="pre">comparator</span></tt> is guaranteed to only be called if <tt class="docutils literal"><span class="pre">check(a,</span> | |
1423 | <span class="pre">b)</span></tt> returns a <tt class="docutils literal"><span class="pre">true</span></tt> value.</p> | |
1424 | </dd> | |
1425 | <dt><tt class="docutils literal"><span class="pre">override</span></tt>:</dt> | |
1426 | <dd>if <tt class="docutils literal"><span class="pre">true</span></tt>, then this will be made the highest precedence | |
1427 | comparator. Otherwise, the lowest.</dd> | |
1428 | <dt><em>Availability</em>:</dt> | |
1429 | <dd>Available in MochiKit 1.3.1+</dd> | |
1430 | </dl> | |
1431 | </blockquote> | |
1432 | <p> | |
1433 | <a name="fn-registerjson"></a> | |
1434 | <a class="mochidef reference" href="#fn-registerjson">registerJSON(name, check, simplifier[, override])</a>:</p> | |
1435 | <blockquote> | |
1436 | <p>Register a simplifier function for use with | |
1437 | <a class="mochiref reference" href="#fn-serializejson">serializeJSON</a>.</p> | |
1438 | <dl class="docutils"> | |
1439 | <dt><tt class="docutils literal"><span class="pre">name</span></tt>:</dt> | |
1440 | <dd>unique identifier describing the serialization.</dd> | |
1441 | <dt><tt class="docutils literal"><span class="pre">check</span></tt>:</dt> | |
1442 | <dd><tt class="docutils literal"><span class="pre">function(obj)</span></tt> that returns <tt class="docutils literal"><span class="pre">true</span></tt> if <tt class="docutils literal"><span class="pre">obj</span></tt> can | |
1443 | can be simplified for serialization by <tt class="docutils literal"><span class="pre">simplifier</span></tt>.</dd> | |
1444 | <dt><tt class="docutils literal"><span class="pre">simplifier</span></tt>:</dt> | |
1445 | <dd><p class="first"><tt class="docutils literal"><span class="pre">function(obj)</span></tt> that returns a simpler object that can be | |
1446 | further serialized by <a class="mochiref reference" href="#fn-serializejson">serializeJSON</a>. For example, | |
1447 | you could simplify <tt class="docutils literal"><span class="pre">Date</span></tt>-like objects to ISO 8601 timestamp | |
1448 | strings with the following simplifier:</p> | |
1449 | <pre class="literal-block"> | |
1450 | var simplifyDateAsISO = function (obj) { | |
1451 | return toISOTimestamp(obj, true); | |
1452 | }; | |
1453 | registerJSON("DateLike", isDateLike, simplifyDateAsISO); | |
1454 | </pre> | |
1455 | <p class="last"><tt class="docutils literal"><span class="pre">simplifier</span></tt> is guaranteed to only be called if | |
1456 | <tt class="docutils literal"><span class="pre">check(obj)</span></tt> returns a <tt class="docutils literal"><span class="pre">true</span></tt> value.</p> | |
1457 | </dd> | |
1458 | <dt><tt class="docutils literal"><span class="pre">override</span></tt>:</dt> | |
1459 | <dd>if <tt class="docutils literal"><span class="pre">true</span></tt>, then this will be made the highest precedence | |
1460 | serialization. Otherwise, the lowest.</dd> | |
1461 | <dt><em>Availability</em>:</dt> | |
1462 | <dd>Available in MochiKit 1.3.1+</dd> | |
1463 | </dl> | |
1464 | </blockquote> | |
1465 | <p> | |
1466 | <a name="fn-registerrepr"></a> | |
1467 | <a class="mochidef reference" href="#fn-registerrepr">registerRepr(name, check, wrap[, override])</a>:</p> | |
1468 | <blockquote> | |
1469 | <p>Register a programmer representation function. <a class="mochiref reference" href="#fn-repr">repr</a> | |
1470 | functions should take one argument and return a string | |
1471 | representation of it suitable for developers, primarily used when | |
1472 | debugging.</p> | |
1473 | <p>If <tt class="docutils literal"><span class="pre">override</span></tt> is given, it is used as the highest priority repr, | |
1474 | otherwise it will be used as the lowest.</p> | |
1475 | <dl class="docutils"> | |
1476 | <dt><em>Availability</em>:</dt> | |
1477 | <dd>Available in MochiKit 1.3.1+</dd> | |
1478 | </dl> | |
1479 | </blockquote> | |
1480 | <p> | |
1481 | <a name="fn-repr"></a> | |
1482 | <a class="mochidef reference" href="#fn-repr">repr(obj)</a>:</p> | |
1483 | <blockquote> | |
1484 | <p>Return a programmer representation for <tt class="docutils literal"><span class="pre">obj</span></tt>. See the | |
1485 | <a class="reference" href="#programmer-representation">Programmer Representation</a> overview for more information about | |
1486 | this function.</p> | |
1487 | <dl class="docutils"> | |
1488 | <dt><em>Availability</em>:</dt> | |
1489 | <dd>Available in MochiKit 1.3.1+</dd> | |
1490 | </dl> | |
1491 | </blockquote> | |
1492 | <p> | |
1493 | <a name="fn-reversekeycomparator"></a> | |
1494 | <a class="mochidef reference" href="#fn-reversekeycomparator">reverseKeyComparator(key)</a>:</p> | |
1495 | <blockquote> | |
1496 | <p>A comparator factory that compares <tt class="docutils literal"><span class="pre">a[key]</span></tt> with <tt class="docutils literal"><span class="pre">b[key]</span></tt> in | |
1497 | reverse. e.g.:</p> | |
1498 | <pre class="literal-block"> | |
1499 | var lst = ["a", "bbb", "cc"]; | |
1500 | lst.sort(reverseKeyComparator("length")); | |
1501 | assert(lst.toString() == "bbb,cc,a"); | |
1502 | </pre> | |
1503 | <dl class="docutils"> | |
1504 | <dt><em>Availability</em>:</dt> | |
1505 | <dd>Available in MochiKit 1.3.1+</dd> | |
1506 | </dl> | |
1507 | </blockquote> | |
1508 | <p> | |
1509 | <a name="fn-serializejson"></a> | |
1510 | <a class="mochidef reference" href="#fn-serializejson">serializeJSON(anObject)</a>:</p> | |
1511 | <blockquote> | |
1512 | <p>Serialize <tt class="docutils literal"><span class="pre">anObject</span></tt> in the JSON <a class="footnote-reference" href="#id7" id="id6" name="id6">[1]</a> format, see <a class="reference" href="#json-serialization">JSON | |
1513 | Serialization</a> for the coercion rules. For unserializable objects | |
1514 | (functions that do not have an adapter, <tt class="docutils literal"><span class="pre">__json__</span></tt> method, or | |
1515 | <tt class="docutils literal"><span class="pre">json</span></tt> method), this will return <tt class="docutils literal"><span class="pre">undefined</span></tt>.</p> | |
1516 | <p>For those familiar with Python, JSON is similar in scope to | |
1517 | pickle, but it can not handle recursive object graphs.</p> | |
1518 | <dl class="docutils"> | |
1519 | <dt><em>Availability</em>:</dt> | |
1520 | <dd>Available in MochiKit 1.3.1+</dd> | |
1521 | </dl> | |
1522 | </blockquote> | |
1523 | <p> | |
1524 | <a name="fn-setdefault"></a> | |
1525 | <a class="mochidef reference" href="#fn-setdefault">setdefault(self, obj[, ...])</a>:</p> | |
1526 | <blockquote> | |
1527 | <p>Mutate <tt class="docutils literal"><span class="pre">self</span></tt> by adding all properties from other object(s) that | |
1528 | it does not already have set.</p> | |
1529 | <p>If <tt class="docutils literal"><span class="pre">self</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, a new <tt class="docutils literal"><span class="pre">Object</span></tt> instance will be created | |
1530 | and returned.</p> | |
1531 | <p>This mutates <em>and returns</em> <tt class="docutils literal"><span class="pre">self</span></tt>, be warned.</p> | |
1532 | <dl class="docutils"> | |
1533 | <dt><em>Availability</em>:</dt> | |
1534 | <dd>Available in MochiKit 1.3.1+</dd> | |
1535 | </dl> | |
1536 | </blockquote> | |
1537 | <p> | |
1538 | <a name="fn-typematcher"></a> | |
1539 | <a class="mochidef reference" href="#fn-typematcher">typeMatcher(typ[, ...])</a>:</p> | |
1540 | <blockquote> | |
1541 | <p>Given a set of types (as string arguments), returns a | |
1542 | <tt class="docutils literal"><span class="pre">function(obj[,</span> <span class="pre">...])</span></tt> that will return <tt class="docutils literal"><span class="pre">true</span></tt> if the types of | |
1543 | the given arguments are all members of that set.</p> | |
1544 | <dl class="docutils"> | |
1545 | <dt><em>Availability</em>:</dt> | |
1546 | <dd>Available in MochiKit 1.3.1+</dd> | |
1547 | </dl> | |
1548 | </blockquote> | |
1549 | <p> | |
1550 | <a name="fn-update"></a> | |
1551 | <a class="mochidef reference" href="#fn-update">update(self, obj[, ...])</a>:</p> | |
1552 | <blockquote> | |
1553 | <p>Mutate <tt class="docutils literal"><span class="pre">self</span></tt> by replacing its key:value pairs with those from | |
1554 | other object(s). Key:value pairs from later objects will overwrite | |
1555 | those from earlier objects.</p> | |
1556 | <p>If <tt class="docutils literal"><span class="pre">self</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, a new <tt class="docutils literal"><span class="pre">Object</span></tt> instance will be created | |
1557 | and returned.</p> | |
1558 | <p>This mutates <em>and returns</em> <tt class="docutils literal"><span class="pre">self</span></tt>, be warned.</p> | |
1559 | <p>A version of this function that creates a new object is available | |
1560 | as <a class="mochiref reference" href="#fn-merge">merge(a, b[, ...])</a></p> | |
1561 | <dl class="docutils"> | |
1562 | <dt><em>Availability</em>:</dt> | |
1563 | <dd>Available in MochiKit 1.3.1+</dd> | |
1564 | </dl> | |
1565 | </blockquote> | |
1566 | <p> | |
1567 | <a name="fn-updatetree"></a> | |
1568 | <a class="mochidef reference" href="#fn-updatetree">updatetree(self, obj[, ...])</a>:</p> | |
1569 | <blockquote> | |
1570 | <p>Mutate <tt class="docutils literal"><span class="pre">self</span></tt> by replacing its key:value pairs with those from | |
1571 | other object(s). If a given key has an object value in both | |
1572 | <tt class="docutils literal"><span class="pre">self</span></tt> and <tt class="docutils literal"><span class="pre">obj</span></tt>, then this function will be called | |
1573 | recursively, updating instead of replacing that object.</p> | |
1574 | <p>If <tt class="docutils literal"><span class="pre">self</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, a new <tt class="docutils literal"><span class="pre">Object</span></tt> instance will be created | |
1575 | and returned.</p> | |
1576 | <p>This mutates <em>and returns</em> <tt class="docutils literal"><span class="pre">self</span></tt>, be warned.</p> | |
1577 | <dl class="docutils"> | |
1578 | <dt><em>Availability</em>:</dt> | |
1579 | <dd>Available in MochiKit 1.3.1+</dd> | |
1580 | </dl> | |
1581 | </blockquote> | |
1582 | <p> | |
1583 | <a name="fn-urlencode"></a> | |
1584 | <a class="mochidef reference" href="#fn-urlencode">urlEncode(unencoded)</a>:</p> | |
1585 | <blockquote> | |
1586 | <p>Converts <tt class="docutils literal"><span class="pre">unencoded</span></tt> into a URL-encoded string. In this | |
1587 | implementation, spaces are converted to %20 instead of "+". e.g.:</p> | |
1588 | <pre class="literal-block"> | |
1589 | assert( URLencode("1+2=2") == "1%2B2%3D2"); | |
1590 | </pre> | |
1591 | <dl class="docutils"> | |
1592 | <dt><em>Availability</em>:</dt> | |
1593 | <dd>Available in MochiKit 1.3.1+</dd> | |
1594 | </dl> | |
1595 | </blockquote> | |
1596 | <p> | |
1597 | <a name="fn-values"></a> | |
1598 | <a class="mochidef reference" href="#fn-values">values(obj)</a>:</p> | |
1599 | <blockquote> | |
1600 | <p>Return an <tt class="docutils literal"><span class="pre">Array</span></tt> of the property values of an object (in the | |
1601 | order determined by <tt class="docutils literal"><span class="pre">for</span> <span class="pre">propName</span> <span class="pre">in</span> <span class="pre">obj</span></tt>).</p> | |
1602 | <dl class="docutils"> | |
1603 | <dt><em>Availability</em>:</dt> | |
1604 | <dd>Available in MochiKit 1.4+</dd> | |
1605 | </dl> | |
1606 | </blockquote> | |
1607 | <p> | |
1608 | <a name="fn-xfilter"></a> | |
1609 | <a class="mochidef reference" href="#fn-xfilter">xfilter(fn, obj[, ...])</a>:</p> | |
1610 | <blockquote> | |
1611 | <p>Returns a new <tt class="docutils literal"><span class="pre">Array</span></tt> composed of the arguments where | |
1612 | <tt class="docutils literal"><span class="pre">fn(obj)</span></tt> returns a true value.</p> | |
1613 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, <tt class="docutils literal"><span class="pre">operator.truth</span></tt> will be used.</p> | |
1614 | <dl class="docutils"> | |
1615 | <dt><em>Availability</em>:</dt> | |
1616 | <dd>Available in MochiKit 1.3.1+</dd> | |
1617 | </dl> | |
1618 | </blockquote> | |
1619 | <p> | |
1620 | <a name="fn-xmap"></a> | |
1621 | <a class="mochidef reference" href="#fn-xmap">xmap(fn, obj[, ...)</a>:</p> | |
1622 | <blockquote> | |
1623 | <p>Return a new <tt class="docutils literal"><span class="pre">Array</span></tt> composed of <tt class="docutils literal"><span class="pre">fn(obj)</span></tt> for every <tt class="docutils literal"><span class="pre">obj</span></tt> | |
1624 | given as an argument.</p> | |
1625 | <p>If <tt class="docutils literal"><span class="pre">fn</span></tt> is <tt class="docutils literal"><span class="pre">null</span></tt>, <tt class="docutils literal"><span class="pre">operator.identity</span></tt> is used.</p> | |
1626 | <dl class="docutils"> | |
1627 | <dt><em>Availability</em>:</dt> | |
1628 | <dd>Available in MochiKit 1.3.1+</dd> | |
1629 | </dl> | |
1630 | </blockquote> | |
1631 | </div> | |
1632 | </div> | |
1633 | <div class="section"> | |
1634 | <h1><a id="see-also" name="see-also">See Also</a></h1> | |
1635 | <table class="docutils footnote" frame="void" id="id7" rules="none"> | |
1636 | <colgroup><col class="label" /><col /></colgroup> | |
1637 | <tbody valign="top"> | |
1638 | <tr><td class="label"><a name="id7">[1]</a></td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id2">2</a>, <a class="fn-backref" href="#id4">3</a>, <a class="fn-backref" href="#id5">4</a>, <a class="fn-backref" href="#id6">5</a>)</em> JSON, JavaScript Object Notation: <a class="reference" href="http://json.org/">http://json.org/</a></td></tr> | |
1639 | </tbody> | |
1640 | </table> | |
1641 | <table class="docutils footnote" frame="void" id="id8" rules="none"> | |
1642 | <colgroup><col class="label" /><col /></colgroup> | |
1643 | <tbody valign="top"> | |
1644 | <tr><td class="label"><a class="fn-backref" href="#id3" name="id8">[2]</a></td><td>Python's itertools | |
1645 | module: <a class="reference" href="http://docs.python.org/lib/module-itertools.html">http://docs.python.org/lib/module-itertools.html</a></td></tr> | |
1646 | </tbody> | |
1647 | </table> | |
1648 | </div> | |
1649 | <div class="section"> | |
1650 | <h1><a id="authors" name="authors">Authors</a></h1> | |
1651 | <ul class="simple"> | |
1652 | <li>Bob Ippolito <<a class="reference" href="mailto:bob@redivi.com">bob@redivi.com</a>></li> | |
1653 | </ul> | |
1654 | </div> | |
1655 | <div class="section"> | |
1656 | <h1><a id="copyright" name="copyright">Copyright</a></h1> | |
1657 | <p>Copyright 2005 Bob Ippolito <<a class="reference" href="mailto:bob@redivi.com">bob@redivi.com</a>>. This program is | |
1658 | dual-licensed free software; you can redistribute it and/or modify it | |
1659 | under the terms of the <a class="reference" href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> or the <a class="reference" href="http://www.opensource.org/licenses/afl-2.1.php">Academic Free License | |
1660 | v2.1</a>.</p> | |
1661 | </div> | |
1662 | </div> | |
1663 | ||
1664 | </body> | |
1665 | </html> |