Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit f38d02b

Browse files
added RESULT_SET as an object inheriting from Array
Added Limit() and ORDER() methods to RESULT_SET
1 parent 785aa66 commit f38d02b

File tree

1 file changed

+165
-24
lines changed

1 file changed

+165
-24
lines changed

src/LocalStorageDB.js

Lines changed: 165 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,73 @@ License: MIT License (see homepage)
3232

3333
// DB innards
3434
DB = {},
35-
AFFECTED_ROWS = 0;
36-
35+
AFFECTED_ROWS = 0,
36+
37+
// The RESULT_SET object inherits from Array
38+
RESULT_SET = function(){};
39+
RESULT_SET.prototype = Array.prototype;
40+
RESULT_SET.prototype.ORDER_BY = function( order )
41+
{
42+
var
43+
arr = this,
44+
l, key, sort;
3745

46+
if ( order == 'RANDOM' )
47+
{
48+
arr.sort(function(){
49+
return 0.5 - Math.random();
50+
});
51+
}
52+
else
53+
{
54+
// convert order to an array
55+
order = order.split(',');
56+
l = order.length;
57+
// loop in reverse order (to keep the specificity correct)
58+
while ( l-- )
59+
{
60+
order[l] = order[l].trim().split(' ');
61+
key = order[l][0];
62+
sort = order[l][1];
63+
arr.sort(function(a,b){
64+
var
65+
ret = 0;
66+
// work on copies
67+
a = clone(a);
68+
b = clone(b);
69+
if ( typeof a[key] == 'string' )
70+
{
71+
a = a[key].toLowerCase();
72+
b = b[key].toLowerCase();
73+
if ( sort == 'ASC' )
74+
{
75+
ret = a < b ? -1 : ( a > b ? 1 : 0 );
76+
}
77+
else
78+
{
79+
ret = a < b ? 1 : ( a > b ? -1 : 0 );
80+
}
81+
}
82+
if ( typeof a[key] == 'number' )
83+
{
84+
ret = sort=='DESC' ? b[key] - a[key] : a[key] - b[key];
85+
}
86+
return ret;
87+
});
88+
}
89+
}
90+
return arr;
91+
};
92+
RESULT_SET.prototype.LIMIT = function( start, end )
93+
{
94+
if ( ! end )
95+
{
96+
end = start;
97+
start = 0;
98+
}
99+
return this.splice( start, end );
100+
};
101+
38102
/**
39103
* init() -> undefined
40104
* Initializes the DB and loads its contents in to memory
@@ -114,26 +178,45 @@ License: MIT License (see homepage)
114178
* Finds items within the data set that match the supplied criteria
115179
*
116180
* @param array d - the data set
117-
* @param object c - the criteria to be matched
181+
* @param mixed c - the criteria object to be matched or the criteria function
118182
*/
119183
function findMatches( d, c )
120184
{
121185
var
186+
d = clone( d ), // never let a select mutate a row
122187
i = d.length,
123-
a = [],
188+
a = new RESULT_SET(),
124189
r, p;
125-
rows: while ( i-- )
190+
if ( c instanceof Function )
126191
{
127-
r = d[i];
128-
for ( p in c )
192+
while ( i-- )
129193
{
130-
if ( c.hasOwnProperty( p ) &&
131-
r[p] != c[p] )
194+
r = c( d[i] );
195+
if ( !! r )
132196
{
133-
continue rows;
197+
a.push( d[i] );
198+
}
199+
}
200+
}
201+
else if ( c instanceof Object )
202+
{
203+
rows: while ( i-- )
204+
{
205+
r = d[i];
206+
for ( p in c )
207+
{
208+
if ( c.hasOwnProperty( p ) &&
209+
r[p] != c[p] )
210+
{
211+
continue rows;
212+
}
134213
}
214+
a.push( r );
135215
}
136-
a.push( r );
216+
}
217+
else if ( ! c )
218+
{
219+
a = d.reverse();
137220
}
138221
return a.reverse();
139222
}
@@ -173,6 +256,14 @@ License: MIT License (see homepage)
173256
{
174257
return writeToCache( table, DB[table] );
175258
}
259+
/**
260+
* clone( obj ) -> object
261+
* Clones a given object
262+
*/
263+
function clone( obj )
264+
{
265+
return decode( encode( obj ) );
266+
}
176267

177268

178269
// ------------------------
@@ -207,6 +298,15 @@ License: MIT License (see homepage)
207298
return AFFECTED_ROWS;
208299
};
209300

301+
/**
302+
* LocalStorageDB.SHOW_TABLES() -> array
303+
* Provides an array of table names
304+
*/
305+
this.SHOW_TABLES = function()
306+
{
307+
return DB[TABLES];
308+
};
309+
210310
/**
211311
* LocalStorageDB.CREATE( table, proto, data ) -> boolean
212312
* Creates a new table and (optionally) in serts data into it
@@ -392,10 +492,15 @@ License: MIT License (see homepage)
392492
* LocalStorageDB.SELECT( table )
393493
* @param str table - the table to read
394494
*
395-
* Option 2: Select based on criteria
495+
* Option 2: Select based on criteria object
396496
* LocalStorageDB.SELECT( table, criteria )
397497
* @param str table - the table to read
398498
* @param obj criteria - the criteria to match
499+
*
500+
* Option 3: Select based on criteria function
501+
* LocalStorageDB.SELECT( table, criteria )
502+
* @param str table - the table to read
503+
* @param function criteria - the function to run against each row
399504
*/
400505
this.SELECT = function( table, criteria )
401506
{
@@ -408,38 +513,74 @@ License: MIT License (see homepage)
408513
throw new Error( table + ' is not a valid table name' );
409514
}
410515
};
411-
516+
517+
412518
/**
413-
* LocalStorageDB.UPDATE( table, data, criteria ) -> undefined
519+
* LocalStorageDB.UPDATE( table, mutation, criteria ) -> undefined
414520
* Updates data in the table
415521
*
416522
* Option 1: Update all rows
417-
* LocalStorageDB.UPDATE( table, data )
523+
* LocalStorageDB.UPDATE( table, mutation )
418524
* @param str table - the table to use
419-
* @param obj data - the data object to use for updating the table
525+
* @param obj mutation - the data object to use for mutating the table
420526
*
421527
* Option 2: Update select rows
422-
* LocalStorageDB.UPDATE( table, data, criteria )
528+
* LocalStorageDB.UPDATE( table, mutation, criteria )
423529
* @param str table - the table to use
424-
* @param obj data - the data object to use for updating the table
530+
* @param obj mutation - the data object to use for mutating the table
425531
* @param obj criteria - the criteria to match
532+
*
533+
* Option 3: Custom mutation
534+
* LocalStorageDB.UPDATE( table, mutation )
535+
* @param str table - the table to use
536+
* @param function mutation - a function for use in mutating the table
426537
*/
427-
this.UPDATE = function( table, data, criteria )
538+
this.UPDATE = function( table, mutation, criteria )
428539
{
429540
AFFECTED_ROWS = 0;
430541
if ( tableExists( table ) )
431542
{
432-
if ( data instanceof Object )
543+
if ( mutation instanceof Function )
544+
{
545+
var
546+
i = DB[table].data.length,
547+
o_data, n_data, p, changed;
548+
while ( i-- )
549+
{
550+
changed = false;
551+
o_data = DB[table].data[i];
552+
n_data = mutation( clone( o_data ) ); // clone before mutating
553+
if ( !! n_data )
554+
{
555+
for ( p in o_data )
556+
{
557+
if ( o_data.hasOwnProperty( p ) &&
558+
o_data[p] != n_data[p] )
559+
{
560+
changed = true;
561+
break;
562+
}
563+
}
564+
if ( changed )
565+
{
566+
DB[table].data[i] = n_data;
567+
AFFECTED_ROWS++;
568+
}
569+
}
570+
}
571+
}
572+
else if ( mutation instanceof Object )
433573
{
434574
withMatches( DB[table].data, criteria, function( i ){
435575
var
436576
newData = DB[table].data[i],
437577
p;
438-
for ( p in data )
578+
for ( p in DB[table].dfn )
439579
{
440-
if ( data.hasOwnProperty( p ) )
580+
if ( DB[table].dfn.hasOwnProperty( p ) &&
581+
mutation.hasOwnProperty( p ) )
441582
{
442-
newData[p] = data[p];
583+
newData[p] = mutation[p];
443584
}
444585
}
445586
DB[table].data[i] = newData;
@@ -448,7 +589,7 @@ License: MIT License (see homepage)
448589
}
449590
else
450591
{
451-
throw new Error( 'LocalStorageDB.insert() expects an Object or an array of Objects to be inserted as data' );
592+
throw new Error( 'LocalStorageDB.UPDATE() expects a mutation object or function as the second argument' );
452593
}
453594
cache( table );
454595
}

0 commit comments

Comments
 (0)