UNPKG

ng-jhipster

Version:

A Jhipster util library for Angular 2

58 lines (57 loc) 2.02 kB
/* * Copyright 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { Injectable } from '@angular/core'; /** * An utility service for link parsing. */ export var ParseLinks = (function () { function ParseLinks() { } /** * Method to parse the links */ ParseLinks.prototype.parse = function (header) { if (header.length === 0) { throw new Error('input must not be of zero length'); } // Split parts by comma var parts = header.split(','); var links = {}; // Parse each part into a named link parts.forEach(function (p) { var section = p.split(';'); if (section.length !== 2) { throw new Error('section could not be split on ";"'); } var url = section[0].replace(/<(.*)>/, '$1').trim(); var queryString = {}; url.replace(new RegExp('([^?=&]+)(=([^&]*))?', 'g'), function ($0, $1, $2, $3) { return queryString[$1] = $3; }); var page = queryString.page; if (typeof page === 'string') { page = parseInt(page, 10); } var name = section[1].replace(/rel="(.*)"/, '$1').trim(); links[name] = page; }); return links; }; ParseLinks.decorators = [ { type: Injectable }, ]; /** @nocollapse */ ParseLinks.ctorParameters = function () { return []; }; return ParseLinks; }());