Skip to content

Commit 5a438e0

Browse files
committed
lint and remove pinName if empty
1 parent 1c9c9f2 commit 5a438e0

13 files changed

+274
-205
lines changed

integration/test/ParseObjectTest.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ describe('Parse Object', () => {
14631463
done();
14641464
}
14651465
});
1466-
1466+
14671467
const controllers = [
14681468
{ name: 'In-Memory', file: require(path.resolve(__dirname, '../../lib/node/LocalDatastoreController.default')) },
14691469
{ name: 'LocalStorage', file: require(path.resolve(__dirname, '../../lib/node/LocalDatastoreController.localStorage')) }
@@ -1527,7 +1527,7 @@ describe('Parse Object', () => {
15271527
object.pin();
15281528
object.set('field', 'new info');
15291529
await object.save();
1530-
1530+
15311531
const localDatastore = Parse.LocalDatastore._getLocalDatastore();
15321532
const cachedObject = localDatastore[object.id];
15331533
assert.equal(Object.keys(localDatastore).length, 2);
@@ -1555,7 +1555,7 @@ describe('Parse Object', () => {
15551555
assert.deepEqual(localDatastore[child.id], child._toFullJSON());
15561556
assert.deepEqual(localDatastore[grandchild.id], grandchild._toFullJSON());
15571557
});
1558-
1558+
15591559
it(`${controller.name} can pinAll (unsaved)`, async () => {
15601560
const obj1 = new TestObject();
15611561
const obj2 = new TestObject();
@@ -1659,7 +1659,7 @@ describe('Parse Object', () => {
16591659
assert(localDatastore[obj2.id]);
16601660

16611661
await obj1.destroy();
1662-
1662+
16631663
localDatastore = Parse.LocalDatastore._getLocalDatastore();
16641664
assert(Object.keys(localDatastore).length === 3);
16651665
assert.deepEqual(localDatastore[DEFAULT_PIN], [obj2.id]);
@@ -1719,7 +1719,7 @@ describe('Parse Object', () => {
17191719
assert.deepEqual(localDatastore[DEFAULT_PIN], [obj1._localId, obj3._localId]);
17201720
assert.deepEqual(localDatastore[obj1._localId], obj1._toFullJSON());
17211721
assert.deepEqual(localDatastore[obj3._localId], obj3._toFullJSON());
1722-
1722+
17231723
await Parse.Object.saveAll(objects);
17241724

17251725
localDatastore = Parse.LocalDatastore._getLocalDatastore();
@@ -1760,15 +1760,13 @@ describe('Parse Object', () => {
17601760
const obj1 = new TestObject();
17611761
obj1.unPin();
17621762
let localDatastore = Parse.LocalDatastore._getLocalDatastore();
1763-
assert.equal(Object.keys(localDatastore).length, 1);
1764-
assert(localDatastore[DEFAULT_PIN]);
1763+
assert.equal(Object.keys(localDatastore).length, 0);
17651764

17661765
const obj2 = new TestObject();
17671766
const obj3 = new TestObject();
17681767
Parse.Object.unPinAll([obj2, obj3]);
17691768
localDatastore = Parse.LocalDatastore._getLocalDatastore();
1770-
assert.equal(Object.keys(localDatastore).length, 1);
1771-
assert(localDatastore[DEFAULT_PIN]);
1769+
assert.equal(Object.keys(localDatastore).length, 0);
17721770
});
17731771

17741772
it(`${controller.name} can unPin / unPinAll without pin (saved)`, async () => {
@@ -1783,15 +1781,13 @@ describe('Parse Object', () => {
17831781

17841782
Parse.Object.unPinAll(objects);
17851783
let localDatastore = Parse.LocalDatastore._getLocalDatastore();
1786-
assert.equal(Object.keys(localDatastore).length, 1);
1787-
assert.deepEqual(localDatastore[DEFAULT_PIN], []);
1784+
assert.equal(Object.keys(localDatastore).length, 0);
17881785

17891786
await unPinObject.save();
17901787
unPinObject.unPin();
17911788

17921789
localDatastore = Parse.LocalDatastore._getLocalDatastore();
1793-
assert.equal(Object.keys(localDatastore).length, 1);
1794-
assert.deepEqual(localDatastore[DEFAULT_PIN], []);
1790+
assert.equal(Object.keys(localDatastore).length, 0);
17951791
});
17961792

17971793
it(`${controller.name} can unPinAll (unsaved)`, async () => {

src/LocalDatastore.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* @flow
1010
*/
1111

12+
/* global localStorage */
13+
1214
import CoreManager from './CoreManager';
1315

1416
import type ParseObject from './ParseObject';
@@ -68,13 +70,17 @@ const LocalDatastore = {
6870
objectIds.push(object._getId());
6971
let pinned = this.fromPinWithName(pinName) || [];
7072
pinned = pinned.filter(item => !objectIds.includes(item));
71-
this.pinWithName(pinName, pinned);
73+
if (pinned.length == 0) {
74+
this.unPinWithName(pinName);
75+
} else {
76+
this.pinWithName(pinName, pinned);
77+
}
7278
},
7379

7480
_getChildren(object: ParseObject) {
7581
const encountered = {};
7682
const json = object._toFullJSON();
77-
for (let key in json) {
83+
for (const key in json) {
7884
if (json[key].__type && json[key].__type === 'Object') {
7985
this._traverse(json[key], encountered);
8086
}
@@ -88,7 +94,7 @@ const LocalDatastore = {
8894
} else {
8995
encountered[object.objectId] = object;
9096
}
91-
for (let key in object) {
97+
for (const key in object) {
9298
if (object[key].__type && object[key].__type === 'Object') {
9399
this._traverse(object[key], encountered);
94100
}
@@ -98,7 +104,7 @@ const LocalDatastore = {
98104
_serializeObjectsFromPinName(name: string) {
99105
const localDatastore = this._getLocalDatastore();
100106
const allObjects = [];
101-
for (let key in localDatastore) {
107+
for (const key in localDatastore) {
102108
if (key !== DEFAULT_PIN && !key.startsWith(PIN_PREFIX)) {
103109
allObjects.push(localDatastore[key]);
104110
}
@@ -131,12 +137,16 @@ const LocalDatastore = {
131137
}
132138
this.unPinWithName(object.id);
133139
const localDatastore = this._getLocalDatastore();
134-
for (let key in localDatastore) {
140+
for (const key in localDatastore) {
135141
if (key === DEFAULT_PIN || key.startsWith(PIN_PREFIX)) {
136142
let pinned = this.fromPinWithName(key) || [];
137143
if (pinned.includes(object.id)) {
138144
pinned = pinned.filter(item => item !== object.id);
139-
this.pinWithName(key, pinned);
145+
if (pinned.length == 0) {
146+
this.unPinWithName(key);
147+
} else {
148+
this.pinWithName(key, pinned);
149+
}
140150
}
141151
}
142152
}
@@ -152,7 +162,7 @@ const LocalDatastore = {
152162

153163
const localDatastore = this._getLocalDatastore();
154164

155-
for (let key in localDatastore) {
165+
for (const key in localDatastore) {
156166
if (key === DEFAULT_PIN || key.startsWith(PIN_PREFIX)) {
157167
let pinned = this.fromPinWithName(key) || [];
158168
if (pinned.includes(localId)) {
@@ -177,7 +187,7 @@ function isLocalStorageEnabled() {
177187
}
178188
LocalDatastore.DEFAULT_PIN = DEFAULT_PIN;
179189
LocalDatastore.PIN_PREFIX = PIN_PREFIX;
180-
LocalDatastore.isLocalStorageEnabled = isLocalStorageEnabled();
190+
LocalDatastore.isLocalStorageEnabled = isLocalStorageEnabled;
181191
module.exports = LocalDatastore;
182192

183193
if (isLocalStorageEnabled()) {

src/LocalDatastoreController.localStorage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* @flow
1010
*/
1111

12+
/* global localStorage */
13+
1214
const LocalDatastoreController = {
1315
fromPinWithName(name: string): ?any {
1416
const values = localStorage.getItem(name);

src/OfflineQuery.js

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -104,118 +104,118 @@ function matchesKeyConstraints(object, key, constraints) {
104104
compareTo = decode(compareTo);
105105
}
106106
switch (condition) {
107-
case '$lt':
108-
if (object[key] >= compareTo) {
109-
return false;
110-
}
111-
break;
112-
case '$lte':
113-
if (object[key] > compareTo) {
114-
return false;
115-
}
116-
break;
117-
case '$gt':
118-
if (object[key] <= compareTo) {
119-
return false;
120-
}
121-
break;
122-
case '$gte':
123-
if (object[key] < compareTo) {
124-
return false;
125-
}
126-
break;
127-
case '$ne':
128-
if (equalObjects(object[key], compareTo)) {
129-
return false;
130-
}
131-
break;
132-
case '$in':
133-
if (!contains(compareTo, object[key])) {
134-
return false;
135-
}
136-
break;
137-
case '$nin':
138-
if (contains(compareTo, object[key])) {
107+
case '$lt':
108+
if (object[key] >= compareTo) {
109+
return false;
110+
}
111+
break;
112+
case '$lte':
113+
if (object[key] > compareTo) {
114+
return false;
115+
}
116+
break;
117+
case '$gt':
118+
if (object[key] <= compareTo) {
119+
return false;
120+
}
121+
break;
122+
case '$gte':
123+
if (object[key] < compareTo) {
124+
return false;
125+
}
126+
break;
127+
case '$ne':
128+
if (equalObjects(object[key], compareTo)) {
129+
return false;
130+
}
131+
break;
132+
case '$in':
133+
if (!contains(compareTo, object[key])) {
134+
return false;
135+
}
136+
break;
137+
case '$nin':
138+
if (contains(compareTo, object[key])) {
139+
return false;
140+
}
141+
break;
142+
case '$all':
143+
for (i = 0; i < compareTo.length; i++) {
144+
if (object[key].indexOf(compareTo[i]) < 0) {
139145
return false;
140146
}
147+
}
148+
break;
149+
case '$exists':
150+
{
151+
const propertyExists = typeof object[key] !== 'undefined';
152+
const existenceIsRequired = constraints['$exists'];
153+
if (typeof constraints['$exists'] !== 'boolean') {
154+
// The SDK will never submit a non-boolean for $exists, but if someone
155+
// tries to submit a non-boolean for $exits outside the SDKs, just ignore it.
141156
break;
142-
case '$all':
143-
for (i = 0; i < compareTo.length; i++) {
144-
if (object[key].indexOf(compareTo[i]) < 0) {
145-
return false;
146-
}
147-
}
148-
break;
149-
case '$exists':
150-
{
151-
const propertyExists = typeof object[key] !== 'undefined';
152-
const existenceIsRequired = constraints['$exists'];
153-
if (typeof constraints['$exists'] !== 'boolean') {
154-
// The SDK will never submit a non-boolean for $exists, but if someone
155-
// tries to submit a non-boolean for $exits outside the SDKs, just ignore it.
156-
break;
157-
}
158-
if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
159-
return false;
160-
}
161-
break;
162-
}
163-
case '$regex':
164-
if (typeof compareTo === 'object') {
165-
return compareTo.test(object[key]);
157+
}
158+
if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
159+
return false;
160+
}
161+
break;
162+
}
163+
case '$regex':
164+
if (typeof compareTo === 'object') {
165+
return compareTo.test(object[key]);
166+
}
167+
// JS doesn't support perl-style escaping
168+
var expString = '';
169+
var escapeEnd = -2;
170+
var escapeStart = compareTo.indexOf('\\Q');
171+
while (escapeStart > -1) {
172+
// Add the unescaped portion
173+
expString += compareTo.substring(escapeEnd + 2, escapeStart);
174+
escapeEnd = compareTo.indexOf('\\E', escapeStart);
175+
if (escapeEnd > -1) {
176+
expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
166177
}
167-
// JS doesn't support perl-style escaping
168-
var expString = '';
169-
var escapeEnd = -2;
170-
var escapeStart = compareTo.indexOf('\\Q');
171-
while (escapeStart > -1) {
172-
// Add the unescaped portion
173-
expString += compareTo.substring(escapeEnd + 2, escapeStart);
174-
escapeEnd = compareTo.indexOf('\\E', escapeStart);
175-
if (escapeEnd > -1) {
176-
expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
177-
}
178178

179-
escapeStart = compareTo.indexOf('\\Q', escapeEnd);
180-
}
181-
expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
182-
var exp = new RegExp(expString, constraints.$options || '');
183-
if (!exp.test(object[key])) {
184-
return false;
185-
}
186-
break;
187-
case '$nearSphere':
188-
if (!compareTo || !object[key]) {
189-
return false;
190-
}
191-
var distance = compareTo.radiansTo(object[key]);
192-
var max = constraints.$maxDistance || Infinity;
193-
return distance <= max;
194-
case '$within':
195-
if (!compareTo || !object[key]) {
196-
return false;
197-
}
198-
var southWest = compareTo.$box[0];
199-
var northEast = compareTo.$box[1];
200-
if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
201-
// Invalid box, crosses the date line
202-
return false;
203-
}
204-
return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
205-
case '$options':
206-
// Not a query type, but a way to add options to $regex. Ignore and
207-
// avoid the default
208-
break;
209-
case '$maxDistance':
210-
// Not a query type, but a way to add a cap to $nearSphere. Ignore and
211-
// avoid the default
212-
break;
213-
case '$select':
179+
escapeStart = compareTo.indexOf('\\Q', escapeEnd);
180+
}
181+
expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
182+
var exp = new RegExp(expString, constraints.$options || '');
183+
if (!exp.test(object[key])) {
214184
return false;
215-
case '$dontSelect':
185+
}
186+
break;
187+
case '$nearSphere':
188+
if (!compareTo || !object[key]) {
216189
return false;
217-
default:
190+
}
191+
var distance = compareTo.radiansTo(object[key]);
192+
var max = constraints.$maxDistance || Infinity;
193+
return distance <= max;
194+
case '$within':
195+
if (!compareTo || !object[key]) {
196+
return false;
197+
}
198+
var southWest = compareTo.$box[0];
199+
var northEast = compareTo.$box[1];
200+
if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
201+
// Invalid box, crosses the date line
218202
return false;
203+
}
204+
return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
205+
case '$options':
206+
// Not a query type, but a way to add options to $regex. Ignore and
207+
// avoid the default
208+
break;
209+
case '$maxDistance':
210+
// Not a query type, but a way to add a cap to $nearSphere. Ignore and
211+
// avoid the default
212+
break;
213+
case '$select':
214+
return false;
215+
case '$dontSelect':
216+
return false;
217+
default:
218+
return false;
219219
}
220220
}
221221
return true;

0 commit comments

Comments
 (0)