source: trunk/phpgwapi/js/tools/xobject.js @ 2665

Revision 2665, 5.1 KB checked in by amuller, 14 years ago (diff)

Ticket #1036 - Arruma ponto e virgula faltante no trim

Line 
1(function( )
2{
3        Object.prototype.extend = function( )
4        {
5                for ( i = 0; i < arguments.length; i++ )
6                {
7                        if ( typeof arguments[ i ] != 'object' || arguments[ i ] === null )
8                                continue;
9
10                        var obj = /(\w+)\(/.exec( arguments[ i ].constructor.toString( ) )[ 1 ];
11                        for ( var property in arguments[ i ] )
12                        {
13                                if ( ! ( this[ property ] ) )
14                                        this[ property ] = arguments[ i ][ property ];
15
16                                this[ '$' + obj + '$' + property ] = arguments[ i ][ property ];
17                        }
18                }
19        };
20
21        String.prototype.trim = function( )
22        {
23                return this.replace( /^ +| +$/g, '' );
24        };
25
26        function f( n )
27        {
28                return n < 10 ? '0' + n : n;
29        }
30
31        Date.prototype.toJSON = function( key )
32        {
33                return isFinite( this.valueOf( ) ) ?
34                this.getUTCFullYear( ) + '-' +
35                f( this.getUTCMonth( ) + 1) + '-' +
36                f( this.getUTCDate( ) ) + 'T' +
37                f( this.getUTCHours( ) ) + ':' +
38                f( this.getUTCMinutes( ) ) + ':' +
39                f( this.getUTCSeconds( ) ) + 'Z' : null;
40        };
41
42        String.prototype.toJSON =
43        Number.prototype.toJSON =
44        Boolean.prototype.toJSON = function( key )
45        {
46                return this.valueOf( );
47        };
48
49        var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
50                escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
51                gap,
52                indent,
53                meta = {
54                        '\b': '\\b',
55                        '\t': '\\t',
56                        '\n': '\\n',
57                        '\f': '\\f',
58                        '\r': '\\r',
59                        '"' : '\\"',
60                        '\\': '\\\\'
61                },
62                rep;
63
64
65        function quote( string )
66        {
67                escapable.lastIndex = 0;
68                return escapable.test( string ) ?
69                        '"' + string.replace(escapable, function (a)
70                        {
71                                var c = meta[ a ];
72                                return typeof c === 'string' ? c :
73                                        '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
74                        } ) + '"' : '"' + string + '"';
75        }
76
77        function str( key, holder )
78        {
79                var i,
80                        k,
81                        v,
82                        length,
83                        mind = gap,
84                        partial,
85                        value = holder[ key ];
86
87                if ( value && typeof value === 'object' && typeof value.toJSON === 'function' )
88                        value = value.toJSON( key );
89
90                if ( typeof rep === 'function' )
91                        value = rep.call( holder, key, value );
92
93                switch ( typeof value )
94                {
95                        case 'string':
96                                return quote(value);
97                        case 'number':
98                                return isFinite(value) ? String(value) : 'null';
99                        case 'boolean':
100                        case 'null':
101                                return String(value);
102                        case 'object':
103                                if ( ! value )
104                                        return 'null';
105
106                                gap += indent;
107                                partial = [ ];
108
109                                if ( Object.prototype.toString.apply( value ) === '[object Array]' )
110                                {
111                                        length = value.length;
112                                        for ( i = 0; i < length; i += 1 )
113                                                partial[i] = str( i, value ) || 'null';
114
115                                        v = partial.length === 0 ? '[]' :
116                                                gap ? '[\n' + gap +
117                                                partial.join( ',\n' + gap ) + '\n' +
118                                                mind + ']' :
119                                                '[' + partial.join( ',' ) + ']';
120
121                                        gap = mind;
122                                        return v;
123                                }
124
125                                if ( rep && typeof rep === 'object' )
126                                {
127                                        length = rep.length;
128                                        for ( i = 0; i < length; i += 1 )
129                                        {
130                                                k = rep[ i ];
131                                                if ( typeof k === 'string' )
132                                                {
133                                                        v = str( k, value );
134
135                                                        if ( v )
136                                                                partial.push( quote( k ) + ( gap ? ': ' : ':' ) + v );
137                                                }
138                                        }
139                                }
140                                else
141                                {
142                                        for ( k in value )
143                                                if ( Object.hasOwnProperty.call( value, k ) )
144                                                {
145                                                        v = str( k, value );
146                                                        if ( v )
147                                                                partial.push( quote( k ) + ( gap ? ': ' : ':' ) + v );
148                                                }
149                                }
150
151                                v = partial.length === 0 ? '{}' :
152                                        gap ? '{\n' + gap + partial.join( ',\n' + gap ) + '\n' +
153                                                mind + '}' : '{' + partial.join( ',' ) + '}';
154                                        gap = mind;
155                                                return v;
156                }
157        }
158
159        function stringify( replacer, space )
160        {
161                var i;
162                gap = '';
163                indent = '';
164
165                if ( typeof space === 'number' )
166                        for ( i = 0; i < space; i += 1 )
167                                indent += ' ';
168                else if ( typeof space === 'string' )
169                        indent = space;
170
171                rep = replacer;
172                if ( replacer && typeof replacer !== 'function'
173                                && ( typeof replacer !== 'object'
174                                        || typeof replacer.length !== 'number' ) )
175                {
176                        throw new Error( 'JSON.stringify' );
177                }
178
179                return str( '', { '': this } );
180        }
181
182        function parse( reviver )
183        {
184                var j;
185
186                function walk( holder, key )
187                {
188                        var k, v, value = holder[ key ];
189                        if ( value && typeof value === 'object' )
190                        {
191                                for ( k in value )
192                                        if ( Object.hasOwnProperty.call( value, k ) )
193                                        {
194                                                v = walk( value, k );
195                                                if ( v !== undefined )
196                                                        value[ k ] = v;
197                                                else
198                                                        delete value[ k ];
199                                        }
200                        }
201                        return reviver.call( holder, key, value );
202                }
203
204                cx.lastIndex = 0;
205                if ( cx.test( this ) )
206                {
207                        this.replace( cx, function( a )
208                        {
209                                return '\\u' + ( '0000' + a.charCodeAt( 0 ).toString( 16 ) ).slice( -4 );
210                        });
211                }
212
213                if ( /^[\],:{}\s]*$/.
214                                test( this.replace( /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@' ).
215                                        replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']' ).
216                                        replace( /(?:^|:|,)(?:\s*\[)+/g, '' ) ) )
217                {
218                        j = eval( '(' + this + ')' );
219
220                        return typeof reviver === 'function' ?
221                                walk( { '': j }, '' ) : j;
222                }
223
224                throw new SyntaxError( 'JSON.parse' );
225        }
226
227        function JSON( )
228        {
229                if ( this.constructor === String )
230                        return parse.call( this );
231                else
232                        return stringify.call( this );
233        }
234
235        Object.prototype.JSON = JSON;
236        String.prototype.JSON = JSON;
237}( ) );
Note: See TracBrowser for help on using the repository browser.