* Returns a new createHistory function that may be used to create * history objects that know how to handle URL queries.
(createHistory)
| 903 | * history objects that know how to handle URL queries. |
| 904 | */ |
| 905 | function useQueries(createHistory) { |
| 906 | return function () { |
| 907 | var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 908 | var stringifyQuery = options.stringifyQuery; |
| 909 | var parseQueryString = options.parseQueryString; |
| 910 | |
| 911 | var historyOptions = _objectWithoutProperties(options, ['stringifyQuery', 'parseQueryString']); |
| 912 | |
| 913 | var history = createHistory(historyOptions); |
| 914 | |
| 915 | if (typeof stringifyQuery !== 'function') stringifyQuery = defaultStringifyQuery; |
| 916 | |
| 917 | if (typeof parseQueryString !== 'function') parseQueryString = defaultParseQueryString; |
| 918 | |
| 919 | function addQuery(location) { |
| 920 | if (location.query == null) { |
| 921 | var search = location.search; |
| 922 | |
| 923 | location.query = parseQueryString(search.substring(1)); |
| 924 | location[SEARCH_BASE_KEY] = { search: search, searchBase: '' }; |
| 925 | } |
| 926 | |
| 927 | // TODO: Instead of all the book-keeping here, this should just strip the |
| 928 | // stringified query from the search. |
| 929 | |
| 930 | return location; |
| 931 | } |
| 932 | |
| 933 | function appendQuery(location, query) { |
| 934 | var _extends2; |
| 935 | |
| 936 | var searchBaseSpec = location[SEARCH_BASE_KEY]; |
| 937 | var queryString = query ? stringifyQuery(query) : ''; |
| 938 | if (!searchBaseSpec && !queryString) { |
| 939 | return location; |
| 940 | } |
| 941 | |
| 942 | false ? _warning2['default'](stringifyQuery !== defaultStringifyQuery || !isNestedObject(query), 'useQueries does not stringify nested query objects by default; ' + 'use a custom stringifyQuery function') : undefined; |
| 943 | |
| 944 | if (typeof location === 'string') location = _PathUtils.parsePath(location); |
| 945 | |
| 946 | var searchBase = undefined; |
| 947 | if (searchBaseSpec && location.search === searchBaseSpec.search) { |
| 948 | searchBase = searchBaseSpec.searchBase; |
| 949 | } else { |
| 950 | searchBase = location.search || ''; |
| 951 | } |
| 952 | |
| 953 | var search = searchBase; |
| 954 | if (queryString) { |
| 955 | search += (search ? '&' : '?') + queryString; |
| 956 | } |
| 957 | |
| 958 | return _extends({}, location, (_extends2 = { |
| 959 | search: search |
| 960 | }, _extends2[SEARCH_BASE_KEY] = { search: search, searchBase: searchBase }, _extends2)); |
| 961 | } |
| 962 |
nothing calls this directly
no test coverage detected
searching dependent graphs…