diff --git a/dist/mobx-async-store.cjs.js b/dist/mobx-async-store.cjs.js index 56b015ca..1592a433 100644 --- a/dist/mobx-async-store.cjs.js +++ b/dist/mobx-async-store.cjs.js @@ -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 () { @@ -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; } diff --git a/dist/mobx-async-store.esm.js b/dist/mobx-async-store.esm.js index b2496988..76ad2d78 100644 --- a/dist/mobx-async-store.esm.js +++ b/dist/mobx-async-store.esm.js @@ -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 () { @@ -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; } diff --git a/package.json b/package.json index fe7e7b11..2c1cc837 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/utils.spec.js b/spec/utils.spec.js index f4b4b5be..4918beff 100644 --- a/spec/utils.spec.js +++ b/spec/utils.spec.js @@ -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) }) diff --git a/src/utils.js b/src/utils.js index b1df7b58..0723e269 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 @@ -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) }