Skip to content

Commit

Permalink
don't break up ids with quirky regex for findMany (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jondayton committed Feb 27, 2021
1 parent 7add5bd commit 4681708
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 50 deletions.
33 changes: 15 additions & 18 deletions dist/mobx-async-store.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function _wrapRegExp(re, groups) { _wrapRegExp = function _wrapRegExp(re, groups
var pending = {};
var counter = {};
var URL_MAX_LENGTH = 1024;
var ENCODED_COMMA = encodeURIComponent(',');

var incrementor = function incrementor(key) {
return function () {
Expand Down Expand Up @@ -286,26 +287,22 @@ function parseErrorPointer() {

function deriveIdQueryStrings(ids) {
var restOfUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var idLength = Math.max.apply(Math, _toConsumableArray(ids.map(function (id) {
return String(id).length;
})));
var maxLength = URL_MAX_LENGTH - restOfUrl.length - encodeURIComponent('filter[ids]=,,').length;
var encodedIds = encodeURIComponent(ids.join(','));

if (encodedIds.length <= maxLength) {
return [ids.join(',')];
}

var minLength = maxLength - idLength;
var regexp = new RegExp(".{".concat(minLength, ",").concat(maxLength, "}%2C"), 'g'); // the matches

var matched = encodedIds.match(regexp); // everything that doesn't match, ie the last of the ids

var tail = encodedIds.replace(regexp, ''); // we manually strip the ',' at the end because javascript's non-capturing regex groups are hard to manage
ids = ids.map(String);
var firstId = ids.shift();
var encodedIds = ids.reduce(function (nestedArray, id) {
var workingString = nestedArray[nestedArray.length - 1];
var longerString = "".concat(workingString).concat(ENCODED_COMMA).concat(id);

if (longerString.length < maxLength) {
nestedArray[nestedArray.length - 1] = longerString;
} else {
nestedArray.push(id);
}

return [].concat(_toConsumableArray(matched), [tail]).map(decodeURIComponent).map(function (string) {
return string.replace(/,$/, '');
});
return nestedArray;
}, [firstId]);
return encodedIds.map(decodeURIComponent);
}

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
Expand Down
33 changes: 15 additions & 18 deletions dist/mobx-async-store.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function _wrapRegExp(re, groups) { _wrapRegExp = function _wrapRegExp(re, groups
var pending = {};
var counter = {};
var URL_MAX_LENGTH = 1024;
var ENCODED_COMMA = encodeURIComponent(',');

var incrementor = function incrementor(key) {
return function () {
Expand Down Expand Up @@ -280,26 +281,22 @@ function parseErrorPointer() {

function deriveIdQueryStrings(ids) {
var restOfUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var idLength = Math.max.apply(Math, _toConsumableArray(ids.map(function (id) {
return String(id).length;
})));
var maxLength = URL_MAX_LENGTH - restOfUrl.length - encodeURIComponent('filter[ids]=,,').length;
var encodedIds = encodeURIComponent(ids.join(','));

if (encodedIds.length <= maxLength) {
return [ids.join(',')];
}

var minLength = maxLength - idLength;
var regexp = new RegExp(".{".concat(minLength, ",").concat(maxLength, "}%2C"), 'g'); // the matches

var matched = encodedIds.match(regexp); // everything that doesn't match, ie the last of the ids

var tail = encodedIds.replace(regexp, ''); // we manually strip the ',' at the end because javascript's non-capturing regex groups are hard to manage
ids = ids.map(String);
var firstId = ids.shift();
var encodedIds = ids.reduce(function (nestedArray, id) {
var workingString = nestedArray[nestedArray.length - 1];
var longerString = "".concat(workingString).concat(ENCODED_COMMA).concat(id);

if (longerString.length < maxLength) {
nestedArray[nestedArray.length - 1] = longerString;
} else {
nestedArray.push(id);
}

return [].concat(_toConsumableArray(matched), [tail]).map(decodeURIComponent).map(function (string) {
return string.replace(/,$/, '');
});
return nestedArray;
}, [firstId]);
return encodedIds.map(decodeURIComponent);
}

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@artemisag/mobx-async-store",
"version": "3.4.0",
"version": "3.4.1",
"module": "dist/mobx-async-store.esm.js",
"browser": "dist/mobx-async-store.cjs.js",
"main": "dist/mobx-async-store.cjs.js",
Expand Down
1 change: 1 addition & 0 deletions spec/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('deriveIdQueryStrings', () => {
it('splits ids into an expected length', () => {
const idQueryStrings = deriveIdQueryStrings(longIds, baseUrl)
expect(idQueryStrings).toHaveLength(8)
expect(longIds.join()).toEqual(idQueryStrings.join().split().join())
idQueryStrings.forEach(ids => {
expect(baseUrl.length + QueryString.stringify({ filter: { ids } }).length).toBeLessThan(URL_MAX_LENGTH)
})
Expand Down
28 changes: 15 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import flattenDeep from 'lodash/flattenDeep'
const pending = {}
const counter = {}
export const URL_MAX_LENGTH = 1024
const ENCODED_COMMA = encodeURIComponent(',')

const incrementor = (key) => () => {
const count = (counter[key] || 0) + 1
Expand Down Expand Up @@ -238,22 +239,23 @@ export function parseErrorPointer (error = {}) {
*/

export function deriveIdQueryStrings (ids, restOfUrl = '') {
const idLength = Math.max(...ids.map(id => String(id).length))
const maxLength = URL_MAX_LENGTH - restOfUrl.length - encodeURIComponent('filter[ids]=,,').length

const encodedIds = encodeURIComponent(ids.join(','))
if (encodedIds.length <= maxLength) {
return [ids.join(',')]
}
ids = ids.map(String)
const firstId = ids.shift()

const encodedIds = ids.reduce((nestedArray, id) => {
const workingString = nestedArray[nestedArray.length - 1]
const longerString = `${workingString}${ENCODED_COMMA}${id}`

const minLength = maxLength - idLength
if (longerString.length < maxLength) {
nestedArray[nestedArray.length - 1] = longerString
} else {
nestedArray.push(id)
}

const regexp = new RegExp(`.{${minLength},${maxLength}}%2C`, 'g')
// the matches
const matched = encodedIds.match(regexp)
// everything that doesn't match, ie the last of the ids
const tail = encodedIds.replace(regexp, '')
return nestedArray
}, [firstId])

// we manually strip the ',' at the end because javascript's non-capturing regex groups are hard to manage
return [...matched, tail].map(decodeURIComponent).map(string => string.replace(/,$/, ''))
return encodedIds.map(decodeURIComponent)
}

0 comments on commit 4681708

Please sign in to comment.