1- // module Data.Argonaut.Core
1+ "use strict" ;
22
33function id ( x ) {
4- return x ;
4+ return x ;
55}
6-
76
8- exports . fromNull = function ( ) {
9- return null ;
7+ exports . fromNull = function ( ) {
8+ return null ;
109} ;
1110
1211exports . fromBoolean = id ;
@@ -17,104 +16,92 @@ exports.fromObject = id;
1716
1817exports . jsonNull = null ;
1918
20- exports . stringify = function ( j ) {
21- return JSON . stringify ( j ) ;
19+ exports . stringify = function ( j ) {
20+ return JSON . stringify ( j ) ;
2221} ;
2322
24- exports . _foldJson = function ( isNull , isBool , isNum , isStr , isArr , isObj , j ) {
25- if ( j == null ) return isNull ( null ) ;
26- else if ( typeof j === 'boolean' ) return isBool ( j ) ;
27- else if ( typeof j === 'number' ) return isNum ( j ) ;
28- else if ( typeof j === 'string' ) return isStr ( j ) ;
29- else if ( Object . prototype . toString . call ( j ) === '[object Array]' )
30- return isArr ( j ) ;
31- else return isObj ( j ) ;
23+ var objToString = Object . prototype . toString ;
24+ var objKeys = Object . keys ;
25+
26+ function isArray ( a ) {
27+ return objToString . call ( a ) === "[object Array]" ;
28+ }
29+
30+ exports . _foldJson = function ( isNull , isBool , isNum , isStr , isArr , isObj , j ) {
31+ if ( j == null ) return isNull ( null ) ;
32+ else if ( typeof j === "boolean" ) return isBool ( j ) ;
33+ else if ( typeof j === "number" ) return isNum ( j ) ;
34+ else if ( typeof j === "string" ) return isStr ( j ) ;
35+ else if ( objToString . call ( j ) === "[object Array]" )
36+ return isArr ( j ) ;
37+ else return isObj ( j ) ;
3238} ;
3339
3440function _compare ( EQ , GT , LT , a , b ) {
35- function isArray ( a ) {
36- return Object . prototype . toString . call ( a ) === '[object Array]' ;
37- }
38- function keys ( o ) {
39- var a = [ ] ;
40- for ( var k in o ) {
41- a . push ( k ) ;
42- }
43- return a ;
44- }
45-
46- if ( a == null ) {
47- if ( b == null ) return EQ ;
48- else return LT ;
49- } else if ( typeof a === 'boolean' ) {
50- if ( typeof b === 'boolean' ) {
51- // boolean / boolean
52- if ( a === b ) return EQ ;
53- else if ( a == false ) return LT ;
54- else return GT ;
55- } else if ( b == null ) return GT ;
56- else return LT ;
57- } else if ( typeof a === 'number' ) {
58- if ( typeof b === 'number' ) {
59- if ( a === b ) return EQ ;
60- else if ( a < b ) return LT ;
61- else return GT ;
62- } else if ( b == null ) return GT ;
63- else if ( typeof b === 'boolean' ) return GT ;
64- else return LT ;
65- } else if ( typeof a === 'string' ) {
66- if ( typeof b === 'string' ) {
67- if ( a === b ) return EQ ;
68- else if ( a < b ) return LT ;
69- else return GT ;
70- } else if ( b == null ) return GT ;
71- else if ( typeof b === 'boolean' ) return GT ;
72- else if ( typeof b === 'number' ) return GT ;
73- else return LT ;
74- } else if ( isArray ( a ) ) {
75- if ( isArray ( b ) ) {
76- for ( var i = 0 ; i < Math . min ( a . length , b . length ) ; i ++ ) {
77- var c = _compare ( EQ , GT , LT , a [ i ] , b [ i ] ) ;
78-
79- if ( c !== EQ ) return c ;
80- }
81- if ( a . length === b . length ) return EQ ;
82- else if ( a . length < b . length ) return LT ;
83- else return GT ;
84- } else if ( b == null ) return GT ;
85- else if ( typeof b === 'boolean' ) return GT ;
86- else if ( typeof b === 'number' ) return GT ;
87- else if ( typeof b === 'string' ) return GT ;
88- else return LT ;
89- }
41+ if ( a == null ) {
42+ if ( b == null ) return EQ ;
43+ else return LT ;
44+ } else if ( typeof a === "boolean" ) {
45+ if ( typeof b === "boolean" ) {
46+ // boolean / boolean
47+ if ( a === b ) return EQ ;
48+ else if ( a === false ) return LT ;
49+ else return GT ;
50+ } else if ( b == null ) return GT ;
51+ else return LT ;
52+ } else if ( typeof a === "number" ) {
53+ if ( typeof b === "number" ) {
54+ if ( a === b ) return EQ ;
55+ else if ( a < b ) return LT ;
56+ else return GT ;
57+ } else if ( b == null ) return GT ;
58+ else if ( typeof b === "boolean" ) return GT ;
59+ else return LT ;
60+ } else if ( typeof a === "string" ) {
61+ if ( typeof b === "string" ) {
62+ if ( a === b ) return EQ ;
63+ else if ( a < b ) return LT ;
64+ else return GT ;
65+ } else if ( b == null ) return GT ;
66+ else if ( typeof b === "boolean" ) return GT ;
67+ else if ( typeof b === "number" ) return GT ;
68+ else return LT ;
69+ } else if ( isArray ( a ) ) {
70+ if ( isArray ( b ) ) {
71+ for ( var i = 0 ; i < Math . min ( a . length , b . length ) ; i ++ ) {
72+ var ca = _compare ( EQ , GT , LT , a [ i ] , b [ i ] ) ;
73+ if ( ca !== EQ ) return ca ;
74+ }
75+ if ( a . length === b . length ) return EQ ;
76+ else if ( a . length < b . length ) return LT ;
77+ else return GT ;
78+ } else if ( b == null ) return GT ;
79+ else if ( typeof b === "boolean" ) return GT ;
80+ else if ( typeof b === "number" ) return GT ;
81+ else if ( typeof b === "string" ) return GT ;
82+ else return LT ;
83+ } else {
84+ if ( b == null ) return GT ;
85+ else if ( typeof b === "boolean" ) return GT ;
86+ else if ( typeof b === "number" ) return GT ;
87+ else if ( typeof b === "string" ) return GT ;
88+ else if ( isArray ( b ) ) return GT ;
9089 else {
91- if ( b == null ) return GT ;
92- else if ( typeof b === 'boolean' ) return GT ;
93- else if ( typeof b === 'number' ) return GT ;
94- else if ( typeof b === 'string' ) return GT ;
95- else if ( isArray ( b ) ) return GT ;
96- else {
97- var akeys = keys ( a ) ;
98- var bkeys = keys ( b ) ;
99-
100- var keys = akeys . concat ( bkeys ) . sort ( ) ;
101-
102- for ( var i = 0 ; i < keys . length ; i ++ ) {
103- var k = keys [ i ] ;
104-
105- if ( a [ k ] === undefined ) return LT ;
106- else if ( b [ k ] === undefined ) return GT ;
107-
108- var c = _compare ( EQ , GT , LT , a [ k ] , b [ k ] ) ;
109-
110- if ( c !== EQ ) return c ;
111- }
112-
113- if ( akeys . length === bkeys . length ) return EQ ;
114- else if ( akeys . length < bkeys . length ) return LT ;
115- else return GT ;
116- }
90+ var akeys = objKeys ( a ) ;
91+ var bkeys = objKeys ( b ) ;
92+ if ( akeys . length < bkeys . length ) return LT ;
93+ else if ( akeys . length > bkeys . length ) return GT ;
94+ var keys = akeys . concat ( bkeys ) . sort ( ) ;
95+ for ( var j = 0 ; j < keys . length ; j ++ ) {
96+ var k = keys [ j ] ;
97+ if ( a [ k ] === undefined ) return LT ;
98+ else if ( b [ k ] === undefined ) return GT ;
99+ var ck = _compare ( EQ , GT , LT , a [ k ] , b [ k ] ) ;
100+ if ( ck !== EQ ) return ck ;
101+ }
102+ return EQ ;
117103 }
118- } ;
104+ }
105+ }
119106
120107exports . _compare = _compare ;
0 commit comments