~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/static/javascript/yui/build/substitute/substitute.js

  • Committer: Paul Hummer
  • Date: 2008-10-20 07:34:21 UTC
  • mto: This revision was merged to the branch mainline in revision 230.
  • Revision ID: paul@ubuntu.com-20081020073421-23tcue15338zryqq
Added forgotten library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.net/yui/license.txt
 
5
version: 3.0.0pr1
 
6
*/
 
7
/**
 
8
 * String variable substitution and string formatting.
 
9
 * If included, the substitute method is added to the YUI instance.
 
10
 *
 
11
 * @module substitute
 
12
 */
 
13
YUI.add("substitute", function(Y) {
 
14
 
 
15
    var L = Y.Lang, DUMP='dump', SPACE=' ', LBRACE='{', RBRACE='}',
 
16
 
 
17
    /**
 
18
     * The following methods are added to the YUI instance
 
19
     * @class YUI~substitute
 
20
     */
 
21
 
 
22
    /**
 
23
     * Does variable substitution on a string. It scans through the string 
 
24
     * looking for expressions enclosed in { } braces. If an expression 
 
25
     * is found, it is used a key on the object.  If there is a space in
 
26
     * the key, the first word is used for the key and the rest is provided
 
27
     * to an optional function to be used to programatically determine the
 
28
     * value (the extra information might be used for this decision). If 
 
29
     * the value for the key in the object, or what is returned from the
 
30
     * function has a string value, number value, or object value, it is 
 
31
     * substituted for the bracket expression and it repeats.  If this
 
32
     * value is an object, it uses the Object's toString() if this has
 
33
     * been overridden, otherwise it does a shallow dump of the key/value
 
34
     * pairs if Y.dump is available (if dump isn't available, toString()
 
35
     * is used).
 
36
     *
 
37
     * This method is included in the 'substitute' module.  It is not included
 
38
     * in the YUI module.
 
39
     *
 
40
     * @method substitute
 
41
     * @param s {string} The string that will be modified.
 
42
     * @param o An object containing the replacement values
 
43
     * @param f {function} An optional function that can be used to
 
44
     *                     process each match.  It receives the key,
 
45
     *                     value, and any extra metadata included with
 
46
     *                     the key inside of the braces.
 
47
     * @return {string} the substituted string
 
48
     */
 
49
    substitute = function (s, o, f) {
 
50
        var i, j, k, key, v, meta, saved=[], token;
 
51
 
 
52
        for (;;) {
 
53
            i = s.lastIndexOf(LBRACE);
 
54
            if (i < 0) {
 
55
                break;
 
56
            }
 
57
            j = s.indexOf(RBRACE, i);
 
58
            if (i + 1 >= j) {
 
59
                break;
 
60
            }
 
61
 
 
62
            //Extract key and meta info 
 
63
            token = s.substring(i + 1, j);
 
64
            key = token;
 
65
            meta = null;
 
66
            k = key.indexOf(SPACE);
 
67
            if (k > -1) {
 
68
                meta = key.substring(k + 1);
 
69
                key = key.substring(0, k);
 
70
            }
 
71
 
 
72
            // lookup the value
 
73
            v = o[key];
 
74
 
 
75
            // if a substitution function was provided, execute it
 
76
            if (f) {
 
77
                v = f(key, v, meta);
 
78
            }
 
79
 
 
80
            if (L.isObject(v)) {
 
81
                if (!Y.dump) {
 
82
                    v = v.toString();
 
83
                } else {
 
84
                    if (L.isArray(v)) {
 
85
                        v = Y.dump(v, parseInt(meta, 10));
 
86
                    } else {
 
87
                        meta = meta || "";
 
88
 
 
89
                        // look for the keyword 'dump', if found force obj dump
 
90
                        var dump = meta.indexOf(DUMP);
 
91
                        if (dump > -1) {
 
92
                            meta = meta.substring(4);
 
93
                        }
 
94
 
 
95
                        // use the toString if it is not the Object toString 
 
96
                        // and the 'dump' meta info was not found
 
97
                        if (v.toString===Object.prototype.toString||dump>-1) {
 
98
                            v = Y.dump(v, parseInt(meta, 10));
 
99
                        } else {
 
100
                            v = v.toString();
 
101
                        }
 
102
                    }
 
103
                }
 
104
            } else if (!L.isString(v) && !L.isNumber(v)) {
 
105
                // This {block} has no replace string. Save it for later.
 
106
                v = "~-" + saved.length + "-~";
 
107
                saved[saved.length] = token;
 
108
 
 
109
                // break;
 
110
            }
 
111
 
 
112
            s = s.substring(0, i) + v + s.substring(j + 1);
 
113
 
 
114
 
 
115
        }
 
116
 
 
117
        // restore saved {block}s
 
118
        for (i=saved.length-1; i>=0; i=i-1) {
 
119
            s = s.replace(new RegExp("~-" + i + "-~"), "{"  + saved[i] + "}", "g");
 
120
        }
 
121
 
 
122
        return s;
 
123
 
 
124
    };
 
125
 
 
126
    Y.substitute = substitute;
 
127
    L.substitute = substitute;
 
128
 
 
129
}, "3.0.0pr1", {
 
130
    optional: ['dump']
 
131
});