Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 5009225

Browse files
authored
Merge pull request #133 from prismicio/fix-array-numbers
Fix any operator with number values
2 parents 8b7e830 + 29cd421 commit 5009225

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

lib/predicates.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
21
"use strict";
32

4-
function clean(s) {
3+
function stripQuotes(s) {
54
return s.replace(/"/g, "");
65
}
76

7+
function toQueryValue(value) {
8+
if (typeof value === 'string') {
9+
return '"' + stripQuotes(value) + '"';
10+
} else if (value instanceof Date) {
11+
return value.getTime();
12+
} else if (Array.isArray(value)) {
13+
return "[" + value.map(function (item) {
14+
return toQueryValue(item);
15+
}).join(',') + "]";
16+
}
17+
return value;
18+
}
19+
820
/**
921
* @global
1022
* @namespace
@@ -16,27 +28,11 @@ module.exports = {
1628
* Convert a predicate (array of 3 elements) into a query for prismic.io (string)
1729
*/
1830
toQuery: function (predicate) {
19-
var pred = clean(predicate[0]);
20-
var first = clean(predicate[1]);
21-
var firstArg = (first.indexOf("my.") === 0 || first.indexOf("document") === 0) ? first
22-
: '"' + first + '"';
23-
return "[:d = " + pred + "(" + firstArg +
24-
(predicate.length > 2 ? ", " : "") +
25-
(function() {
26-
return predicate.slice(2).map(function(p) {
27-
if (typeof p === 'string') {
28-
return '"' + clean(p) + '"';
29-
} else if (Array.isArray(p)) {
30-
return "[" + p.map(function (e) {
31-
return '"' + clean(e) + '"';
32-
}).join(',') + "]";
33-
} else if (p instanceof Date) {
34-
return p.getTime();
35-
} else {
36-
return p;
37-
}
38-
}).join(',');
39-
})() + ")]";
31+
var operator = stripQuotes(predicate[0]);
32+
var path = stripQuotes(predicate[1]);
33+
var pathArg = (path.indexOf("my.") === 0 || path.indexOf("document") === 0) ? path : '"' + path + '"';
34+
var values = predicate.slice(2).map(toQueryValue).join(',');
35+
return "[:d = " + operator + "(" + pathArg + (predicate.length > 2 ? ", " : "") + values + ")]";
4036
},
4137

4238
/**

test/test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ var chai = require('chai');
66

77
var assert = chai.assert;
88

9-
/* === TESTS ARE RUN OVER "LES BONNES CHOSES" EXAMPLE REPOSITORY === */
10-
119
var microRepository = 'https://micro.prismic.io/api',
1210
previewToken = 'MC5VcXBHWHdFQUFONDZrbWp4.77-9cDx6C3lgJu-_vXZafO-_vXPvv73vv73vv70777-9Ju-_ve-_vSLvv73vv73vv73vv70O77-977-9Me-_vQ',
1311
Predicates = Prismic.Predicates;
@@ -122,7 +120,7 @@ describe('API form submissions', function() {
122120
});
123121
});
124122

125-
it('Use an Array to query', function (done) {
123+
it('Use an Array of String to query', function (done) {
126124
Prismic.api(microRepository, function (err, Api) {
127125
if (err) {
128126
console.log(err);
@@ -143,6 +141,25 @@ describe('API form submissions', function() {
143141
});
144142
});
145143

144+
it('Use an Array of Number to query', function (done) {
145+
Prismic.api(microRepository, function (err, Api) {
146+
if (err) {
147+
console.log(err);
148+
done();
149+
}
150+
Api.form('everything')
151+
.query(Predicates.any('my.argument.priority', [1000, 600]))
152+
.ref(Api.master())
153+
.submit(function (err, response) {
154+
if (err) {
155+
console.log(err);
156+
}
157+
assert.equal(response.results.length, 2);
158+
done();
159+
});
160+
});
161+
});
162+
146163
it('Use getByID', function(done) {
147164
Prismic.api(microRepository, function (err, Api) {
148165
if (err) throw err;

0 commit comments

Comments
 (0)