📄 base.rst
字号:
| ``identity(a)`` | ``a`` | Logical identity | +--------------------+--------------------------+-------------------+ Unary Math Operators: +--------------------+--------------------------+---------------+ | Operator | Implementation | Description | +====================+==========================+===============+ | ``not(a)`` | ``~a`` | Bitwise not | +--------------------+--------------------------+---------------+ | ``neg(a)`` | ``-a`` | Negation | +--------------------+--------------------------+---------------+ Binary Operators: +-------------------+-------------------+-------------------------------+ | Operator | Implementation | Description | +===================+===================+===============================+ | ``add(a, b)`` | ``a + b`` | Addition | +-------------------+-------------------+-------------------------------+ | ``sub(a, b)`` | ``a - b`` | Subtraction | +-------------------+-------------------+-------------------------------+ | ``div(a, b)`` | ``a / b`` | Division | +-------------------+-------------------+-------------------------------+ | ``mod(a, b)`` | ``a % b`` | Modulus | +-------------------+-------------------+-------------------------------+ | ``mul(a, b)`` | ``a * b`` | Multiplication | +-------------------+-------------------+-------------------------------+ | ``and(a, b)`` | ``a & b`` | Bitwise and | +-------------------+-------------------+-------------------------------+ | ``or(a, b)`` | ``a | b`` | Bitwise or | +-------------------+-------------------+-------------------------------+ | ``xor(a, b)`` | ``a ^ b`` | Bitwise exclusive or | +-------------------+-------------------+-------------------------------+ | ``lshift(a, b)`` | ``a << b`` | Bitwise left shift | +-------------------+-------------------+-------------------------------+ | ``rshift(a, b)`` | ``a >> b`` | Bitwise signed right shift | +-------------------+-------------------+-------------------------------+ | ``zrshfit(a, b)`` | ``a >>> b`` | Bitwise unsigned right shift | +-------------------+-------------------+-------------------------------+ Built-in Comparators: +---------------+-------------------+---------------------------+ | Operator | Implementation | Description | +===============+===================+===========================+ | ``eq(a, b)`` | ``a == b`` | Equals | +---------------+-------------------+---------------------------+ | ``ne(a, b)`` | ``a != b`` | Not equals | +---------------+-------------------+---------------------------+ | ``gt(a, b)`` | ``a > b`` | Greater than | +---------------+-------------------+---------------------------+ | ``ge(a, b)`` | ``a >= b`` | Greater than or equal to | +---------------+-------------------+---------------------------+ | ``lt(a, b)`` | ``a < b`` | Less than | +---------------+-------------------+---------------------------+ | ``le(a, b)`` | ``a <= b`` | Less than or equal to | +---------------+-------------------+---------------------------+ Extended Comparators (uses :mochiref:`compare`): +---------------+---------------------------+---------------------------+ | Operator | Implementation | Description | +===============+===========================+===========================+ | ``ceq(a, b)`` | ``compare(a, b) == 0`` | Equals | +---------------+---------------------------+---------------------------+ | ``cne(a, b)`` | ``compare(a, b) != 0`` | Not equals | +---------------+---------------------------+---------------------------+ | ``cgt(a, b)`` | ``compare(a, b) == 1`` | Greater than | +---------------+---------------------------+---------------------------+ | ``cge(a, b)`` | ``compare(a, b) != -1`` | Greater than or equal to | +---------------+---------------------------+---------------------------+ | ``clt(a, b)`` | ``compare(a, b) == -1`` | Less than | +---------------+---------------------------+---------------------------+ | ``cle(a, b)`` | ``compare(a, b) != 1`` | Less than or equal to | +---------------+---------------------------+---------------------------+ Binary Logical Operators: +-----------------------+-------------------+---------------------------+ | Operator | Implementation | Description | +=======================+===================+===========================+ | ``logand(a, b)`` | ``a && b`` | Logical and | +-----------------------+-------------------+---------------------------+ | ``logor(a, b)`` | ``a || b`` | Logical or | +-----------------------+-------------------+---------------------------+ | ``contains(a, b)`` | ``b in a`` | Has property (note order) | +-----------------------+-------------------+---------------------------+:mochidef:`parseQueryString(encodedString[, useArrays=false])`: Parse a name=value pair URL query string into an object with a property for each pair. e.g.:: var args = parseQueryString("foo=value%20one&bar=two"); assert( args.foo == "value one" && args.bar == "two" ); If you expect that the query string will reuse the same name, then give ``true`` as a second argument, which will use arrays to store the values. e.g.:: var args = parseQueryString("foo=one&foo=two", true); assert( args.foo[0] == "one" && args.foo[1] == "two" );:mochidef:`partial(func, arg[, ...])`: Return a partially applied function, e.g.:: addNumbers = function (a, b) { return a + b; } addOne = partial(addNumbers, 1); assert(addOne(2) == 3); :mochiref:`partial` is a special form of :mochiref:`bind` that does not alter the bound ``self`` (if any). It is equivalent to calling:: bind(func, undefined, arg[, ...]); See the documentation for :mochiref:`bind` for more details about this facility. This could be used to implement, but is NOT currying. :mochidef:`queryString(names, values)`: Creates a URL query string from a pair of ``Array``-like objects representing ``names`` and ``values``. Each name=value pair will be URL encoded by :mochiref:`urlEncode`. name=value pairs with a value of ``undefined`` or ``null`` will be skipped. e.g.:: var keys = ["foo", "bar"]; var values = ["value one", "two"]; assert( queryString(keys, values) == "foo=value%20one&bar=two" ); Alternate form 1: :mochiref:`queryString(domElement)` If :mochiref:`MochiKit.DOM` is loaded, one argument is given, and that argument is either a string or has a ``nodeType`` property greater than zero, then ``names`` and ``values`` will be the result of :mochiref:`MochiKit.DOM.formContents(domElement)`. Alternate form 2: :mochiref:`queryString({name: value, ...})` Note that when using the alternate form, the order of the name=value pairs in the resultant query string is dependent on how the particular JavaScript implementation handles ``for (..in..)`` property enumeration. When using the second alternate form, name=value pairs with ``typeof(value) == "function"`` are ignored. This is a workaround for the case where a poorly designed library has modified ``Object.prototype`` and inserted "convenience functions".:mochidef:`registerComparator(name, check, comparator[, override])`: Register a comparator for use with :mochiref:`compare`. ``name``: unique identifier describing the comparator. ``check``: ``function(a, b)`` that returns ``true`` if ``a`` and ``b`` can be compared with ``comparator``. ``comparator``: ``function(a, b)`` that returns: +-------+-----------+ | Value | Condition | +-------+-----------+ | 0 | a == b | +-------+-----------+ | 1 | a > b | +-------+-----------+ | -1 | a < b | +-------+-----------+ ``comparator`` is guaranteed to only be called if ``check(a, b)`` returns a ``true`` value. ``override``: if ``true``, then this will be made the highest precedence comparator. Otherwise, the lowest.:mochidef:`registerJSON(name, check, simplifier[, override])`: Register a simplifier function for use with :mochiref:`serializeJSON`. ``name``: unique identifier describing the serialization. ``check``: ``function(obj)`` that returns ``true`` if ``obj`` can can be simplified for serialization by ``simplifier``. ``simplifier``: ``function(obj)`` that returns a simpler object that can be further serialized by :mochiref:`serializeJSON`. For example, you could simplify ``Date``-like objects to ISO 8601 timestamp strings with the following simplifier:: var simplifyDateAsISO = function (obj) { return toISOTimestamp(obj, true); }; registerJSON("DateLike", isDateLike, simplifyDateAsISO); ``simplifier`` is guaranteed to only be called if ``check(obj)`` returns a ``true`` value. ``override``: if ``true``, then this will be made the highest precedence comparator. Otherwise, the lowest.:mochidef:`registerRepr(name, check, wrap[, override])`: Register a programmer representation function. :mochiref:`repr` functions should take one argument and return a string representation of it suitable for developers, primarily used when debugging. If ``override`` is given, it is used as the highest priority repr, otherwise it will be used as the lowest.:mochidef:`repr(obj)`: Return a programmer representation for ``obj``. See the `Programmer Representation`_ overview for more information about this function.:mochidef:`reverseKeyComparator(key)`: A comparator factory that compares ``a[key]`` with ``b[key]`` in reverse. e.g.:: var lst = ["a", "bbb", "cc"]; lst.sort(reverseKeyComparator("length")); assert(lst.toString() == "bbb,cc,a");:mochidef:`serializeJSON(anObject)`: Serialize ``anObject`` in the JSON [1]_ format, see `JSON Serialization`_ for the coercion rules. For unserializable objects (functions that do not have an adapter, ``__json__`` method, or ``json`` method), this will return ``undefined``. For those familiar with Python, JSON is similar in scope to pickle, but it can not handle recursive object graphs.:mochidef:`setdefault(self, obj[, ...])`: Mutate ``self`` by adding all properties from other object(s) that it does not already have set. If ``self`` is ``null``, a new ``Object`` instance will be created and returned. This mutates *and returns* ``self``, be warned.:mochidef:`typeMatcher(typ[, ...])`: Given a set of types (as string arguments), returns a ``function(obj[, ...])`` that will return ``true`` if the types of the given arguments are all members of that set.:mochidef:`update(self, obj[, ...])`: Mutate ``self`` by replacing its key:value pairs with those from other object(s). Key:value pairs from later objects will overwrite those from earlier objects. If ``self`` is ``null``, a new ``Object`` instance will be created and returned. This mutates *and returns* ``self``, be warned. A version of this function that creates a new object is available as :mochiref:`merge(a, b[, ...])`:mochidef:`updatetree(self, obj[, ...])`: Mutate ``self`` by replacing its key:value pairs with those from other object(s). If a given key has an object value in both ``self`` and ``obj``, then this function will be called recursively, updating instead of replacing that object. If ``self`` is ``null``, a new ``Object`` instance will be created and returned. This mutates *and returns* ``self``, be warned. :mochidef:`urlEncode(unencoded)`: Converts ``unencoded`` into a URL-encoded string. In this implementation, spaces are converted to %20 instead of "+". e.g.:: assert( URLencode("1+2=2") == "1%2B2%3D2");:mochidef:`xfilter(fn, obj[, ...])`: Returns a new ``Array`` composed of the arguments where ``fn(obj)`` returns a true value. If ``fn`` is ``null``, ``operator.truth`` will be used.:mochidef:`xmap(fn, obj[, ...)`: Return a new ``Array`` composed of ``fn(obj)`` for every ``obj`` given as an argument. If ``fn`` is ``null``, ``operator.identity`` is used.See Also========.. [1] JSON, JavaScript Object Notation: http://json.org/.. [2] Python's itertools module: http://docs.python.org/lib/module-itertools.htmlAuthors=======- Bob Ippolito <bob@redivi.com>Copyright=========Copyright 2005 Bob Ippolito <bob@redivi.com>. This program is dual-licensedfree software; you can redistribute it and/or modify it under the terms of the`MIT License`_ or the `Academic Free License v2.1`_... _`MIT License`: http://www.opensource.org/licenses/mit-license.php.. _`Academic Free License v2.1`: http://www.opensource.org/licenses/afl-2.1.php
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -