corrade-nucleus-nucleons – Blame information for rev 26

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 <script type="text/javascript">
2  
3 /*
4 var o = [];
5 o[1] = 'a';
6 o[2] = 'b';
7 o['x'] = 'd';
8 o[5] = 'c';
9 o['t'] = 'e';
10  
11 o.map(function(value,i){
12 if (i != undefined && !isNaN(parseFloat(i)) && isFinite(i)) return i; })
13 .reduce(function(prev, item){
14 if (item) return item;
15 })
16  
17 */
18  
19 'use strict';
20  
21 function isNumeric( obj ) {
22 return !isNaN( parseFloat(obj) ) && isFinite( obj );
23 }
24  
25 function length(o){
26  
27 var l, k;
28  
29 if( typeof Object.keys === 'function' ) {
30 l = Object.keys(o).length;
31 } else {
32 for (k in o) {
33 if (o.hasOwnProperty(k)) {
34 l++;
35 }
36 }
37 }
38  
39 return l;
40  
41 }
42  
43 function getObjectKeys(o){
44 var r = [], k;
45  
46 for (k in o) {
47 if (o.hasOwnProperty(k)) {
48 r.push(k);
49 }
50 }
51  
52 return r;
53  
54 }
55  
56 function getLastKey(o){
57 return Object.keys(o).slice(-1)[0];
58 }
59  
60 function getLastNumericKey(o){
61 return Object.keys(o).filter(function(elem){
62 return !isNaN(parseInt(elem,10));
63 }).splice(-1)[0];
64 }
65  
66 function abc(arr, value, result){
67  
68 var keyName = arr[0];
69  
70 if(arr.length > 1){
71 if( keyName === '[]' ){
72 result.push([]);
73 return abc(arr.splice(1, arr.length), value, result[getLastNumericKey(result)]);
74 } else {
75  
76 if( result[keyName] && length(result[keyName]) > 0 ) {
77 //result[keyName].push(null);
78 return abc(arr.splice(1, arr.length), value, result[keyName]);
79 } else {
80 result[keyName] = [];
81 }
82 return abc(arr.splice(1, arr.length), value, result[keyName]);
83 }
84  
85 }
86  
87 // Last key, attach the original value.
88 if(arr.length === 1){
89 if( keyName === '[]' ){
90 result.push(value);
91 return result;
92 } else {
93 result[keyName] = value;
94 return result;
95 }
96 }
97  
98 }
99  
100 var result = [];
101 abc(['name'], 'Serban Ghita', result);
102  
103 /*
104 // name="matrix[][a][b][c]" value="Like abc!"
105 // name="matrix[][d][e][f]" value="Like def!"
106 // name="matrix[][]" value="Like 2 0!"
107 // name="matrix[2][]" value="Like 2 1!"
108  
109 var result = [];
110 abc(['[]', 'a','b','c'], 'Like 0abc!', result);
111 abc(['[]', 'd','e','f'], 'Like 1def!', result);
112 abc(['0', 'a','b','d'], 'Like 0abd!', result);
113 abc(['[]', '[]'], 'Like 2 0!', result);
114 abc(['2', '1'], 'Like 2 1!', result);
115 abc(['2', '[]'], 'Like 2 2!', result);
116 abc(['2', '4'], 'Like 2 4!', result);
117 abc(['x', '2'], 'Like x 2!', result);
118 abc(['x', '3'], 'Like x 3!', result);
119 abc(['x', '5'], 'Like x 5!', result);
120 abc(['[]', '[]', '[]'], 'Like ?00!', result);
121 abc(['[]', '[]', '[]'], 'Like ?10!', result);
122 abc(['[]', '[]', '[]'], 'Like ?20!', result);
123 */
124 console.log('final result:', result);
125  
126  
127 </script>