url-transformers
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -9,6 +9,6 @@ { | ||
}, | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"scripts": { | ||
"compile": "rm -rf ./target/ && tsc", | ||
"test": "npm run compile && node ./target/tests.js", | ||
"test": "npm run compile && node --require source-map-support/register ./target/tests.js", | ||
"format": "prettier --write './**/*.{ts,js,json,md}' '.prettierrc'", | ||
@@ -21,8 +21,10 @@ "prepublishOnly": "npm run compile" | ||
"dependencies": { | ||
"@types/node": "^10.12.10" | ||
"@types/node": "^12.12.16", | ||
"pipe-ts": "^0.0.8" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^1.15.3", | ||
"source-map-support": "^0.5.16", | ||
"typescript": "^3.2.1" | ||
} | ||
} |
@@ -7,4 +7,4 @@ # `url-transformers` | ||
- `replaceQueryInUrl` | ||
- `addQueryToUrl` | ||
- `replaceQueryInUrl` | ||
- `replacePathInUrl` | ||
@@ -11,0 +11,0 @@ - `replacePathnameInUrl` |
@@ -1,5 +0,5 @@ | ||
declare type Maybe<T> = undefined | T; | ||
export declare const isNotUndefined: <T>(maybeT: Maybe<T>) => maybeT is T; | ||
declare type Maybe<T> = undefined | null | T; | ||
export declare const isDefined: <T>(maybeT: Maybe<T>) => maybeT is T; | ||
export declare const mapMaybe: <T, B extends {}>(maybeT: Maybe<T>, f: (t: T) => B) => Maybe<B>; | ||
export declare const getOrElseMaybe: <T>(maybeT: Maybe<T>, fallback: () => T) => T; | ||
export {}; |
"use strict"; | ||
// https://github.com/OliverJAsh/simple-maybe | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isNotUndefined = function (maybeT) { return maybeT !== undefined; }; | ||
exports.isDefined = function (maybeT) { | ||
return maybeT !== undefined && maybeT !== null; | ||
}; | ||
// extend {} to ensure we're mapping to a non-null type | ||
exports.mapMaybe = function (maybeT, f) { | ||
return exports.isNotUndefined(maybeT) ? f(maybeT) : maybeT; | ||
return exports.isDefined(maybeT) ? f(maybeT) : maybeT; | ||
}; | ||
exports.getOrElseMaybe = function (maybeT, fallback) { | ||
return exports.isNotUndefined(maybeT) ? maybeT : fallback(); | ||
return exports.isDefined(maybeT) ? maybeT : fallback(); | ||
}; | ||
//# sourceMappingURL=maybe.js.map |
@@ -1,24 +0,51 @@ | ||
declare type ParsedUrlQueryInput = { | ||
[key: string]: unknown; | ||
}; | ||
export declare const addQueryToUrl: (b: { | ||
import { ParsedUrlQueryInput } from 'querystring'; | ||
import * as urlHelpers from 'url'; | ||
interface NodeUrlObjectWithParsedQuery extends urlHelpers.UrlObject { | ||
query: ParsedUrlQueryInput | null; | ||
} | ||
declare type Update<T> = T | ((prev: T) => T); | ||
declare type ParsedUrl = Required<Pick<NodeUrlObjectWithParsedQuery, 'auth' | 'hash' | 'hostname' | 'pathname' | 'port' | 'protocol' | 'query' | 'slashes'>>; | ||
declare type MapParsedUrlFn = ({ parsedUrl }: { | ||
parsedUrl: ParsedUrl; | ||
}) => ParsedUrl; | ||
export declare const mapParsedUrl: (fn: MapParsedUrlFn) => MapParsedUrlFn; | ||
declare type MapUrlFn = ({ url }: { | ||
url: string; | ||
}) => (a: { | ||
queryToAppend: ParsedUrlQueryInput; | ||
}) => string; | ||
export declare const mapUrl: (fn: MapParsedUrlFn) => MapUrlFn; | ||
export declare const replaceQueryInParsedUrl: ({ newQuery, }: { | ||
newQuery: Update<ParsedUrlQueryInput | null>; | ||
}) => MapParsedUrlFn; | ||
export declare const replaceQueryInUrl: (b: { | ||
url: string; | ||
}) => (a: { | ||
newQuery: ParsedUrlQueryInput; | ||
newQuery: Update<ParsedUrlQueryInput | null>; | ||
}) => string; | ||
export declare const addQueryToParsedUrl: ({ queryToAppend, }: { | ||
queryToAppend: ParsedUrlQueryInput | null; | ||
}) => MapParsedUrlFn; | ||
export declare const addQueryToUrl: (b: { | ||
url: string; | ||
}) => (a: { | ||
queryToAppend: ParsedUrlQueryInput | null; | ||
}) => string; | ||
export declare const replacePathInParsedUrl: ({ newPath, }: { | ||
newPath: Update<string | null | undefined>; | ||
}) => MapParsedUrlFn; | ||
export declare const replacePathInUrl: (b: { | ||
url: string; | ||
}) => (a: { | ||
newPath: string; | ||
newPath: Update<string | null | undefined>; | ||
}) => string; | ||
export declare const replacePathnameInParsedUrl: ({ newPathname, }: { | ||
newPathname: Update<string | null>; | ||
}) => MapParsedUrlFn; | ||
export declare const replacePathnameInUrl: (b: { | ||
url: string; | ||
}) => (a: { | ||
newPathname: string; | ||
newPathname: Update<string | null>; | ||
}) => string; | ||
export declare const appendPathnameToParsedUrl: ({ pathnameToAppend, }: { | ||
pathnameToAppend: string; | ||
}) => MapParsedUrlFn; | ||
export declare const appendPathnameToUrl: (b: { | ||
@@ -29,7 +56,10 @@ url: string; | ||
}) => string; | ||
export declare const replaceHashInParsedUrl: ({ newHash, }: { | ||
newHash: Update<string | null>; | ||
}) => MapParsedUrlFn; | ||
export declare const replaceHashInUrl: (b: { | ||
url: string; | ||
}) => (a: { | ||
newHash: string | undefined; | ||
newHash: Update<string | null>; | ||
}) => string; | ||
export {}; |
@@ -14,6 +14,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var pipe_ts_1 = require("pipe-ts"); | ||
var urlHelpers = require("url"); | ||
var maybe_1 = require("./helpers/maybe"); | ||
var other_1 = require("./helpers/other"); | ||
var pipe_1 = require("./helpers/pipe"); | ||
var getPathnameFromParts = function (parts) { return "/" + parts.join('/'); }; | ||
@@ -26,87 +26,86 @@ var getPartsFromPathname = function (pathname) { return pathname.split('/').filter(other_1.isNonEmptyString); }; | ||
}; | ||
var mapUrl = function (fn) { | ||
return pipe_1.pipe(function (_a) { | ||
var url = _a.url; | ||
return urlHelpers.parse(url); | ||
}, function (parsedUrl) { return fn({ parsedUrl: parsedUrl }); }, urlHelpers.format); | ||
var convertNodeUrl = function (_a) { | ||
var auth = _a.auth, hash = _a.hash, hostname = _a.hostname, pathname = _a.pathname, port = _a.port, protocol = _a.protocol, query = _a.query, slashes = _a.slashes; | ||
return ({ | ||
auth: auth, | ||
hash: hash, | ||
hostname: hostname, | ||
pathname: pathname, | ||
port: port, | ||
protocol: protocol, | ||
query: query, | ||
slashes: slashes, | ||
}); | ||
}; | ||
var mapUrlWithParsedQuery = function (fn) { | ||
return pipe_1.pipe(function (_a) { | ||
exports.mapParsedUrl = function (fn) { return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return fn({ parsedUrl: parsedUrl }); | ||
}; }; | ||
exports.mapUrl = function (fn) { | ||
return pipe_ts_1.pipe(function (_a) { | ||
var url = _a.url; | ||
return parseUrlWithQueryString(url); | ||
}, function (parsedUrl) { return fn({ parsedUrl: parsedUrl }); }, urlHelpers.format); | ||
}, convertNodeUrl, function (parsedUrl) { return fn({ parsedUrl: parsedUrl }); }, urlHelpers.format); | ||
}; | ||
var addQueryToParsedUrl = function (_a) { | ||
var queryToAppend = _a.queryToAppend; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
var auth = parsedUrl.auth, protocol = parsedUrl.protocol, host = parsedUrl.host, hash = parsedUrl.hash, pathname = parsedUrl.pathname, existingQuery = parsedUrl.query; | ||
var newQuery = __assign({}, existingQuery, queryToAppend); | ||
return { | ||
auth: auth, | ||
protocol: protocol, | ||
host: host, | ||
hash: hash, | ||
pathname: pathname, | ||
query: newQuery, | ||
}; | ||
}; | ||
}; | ||
exports.addQueryToUrl = other_1.flipCurried(pipe_1.pipe(addQueryToParsedUrl, mapUrlWithParsedQuery)); | ||
var replaceQueryInParsedUrl = function (_a) { | ||
exports.replaceQueryInParsedUrl = function (_a) { | ||
var newQuery = _a.newQuery; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
var auth = parsedUrl.auth, protocol = parsedUrl.protocol, host = parsedUrl.host, hash = parsedUrl.hash, pathname = parsedUrl.pathname; | ||
return { | ||
auth: auth, | ||
protocol: protocol, | ||
host: host, | ||
hash: hash, | ||
pathname: pathname, | ||
query: newQuery, | ||
}; | ||
return (__assign({}, parsedUrl, { query: newQuery instanceof Function ? newQuery(parsedUrl.query) : newQuery })); | ||
}; | ||
}; | ||
exports.replaceQueryInUrl = other_1.flipCurried(pipe_1.pipe(replaceQueryInParsedUrl, mapUrlWithParsedQuery)); | ||
var parsePath = pipe_1.pipe(urlHelpers.parse, function (_a) { | ||
var search = _a.search, pathname = _a.pathname; | ||
return ({ search: search, pathname: pathname }); | ||
exports.replaceQueryInUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.replaceQueryInParsedUrl, exports.mapUrl)); | ||
exports.addQueryToParsedUrl = function (_a) { | ||
var queryToAppend = _a.queryToAppend; | ||
return exports.replaceQueryInParsedUrl({ | ||
newQuery: function (existingQuery) { return (__assign({}, existingQuery, queryToAppend)); }, | ||
}); | ||
}; | ||
exports.addQueryToUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.addQueryToParsedUrl, exports.mapUrl)); | ||
var parsePath = pipe_ts_1.pipe(function (path) { return urlHelpers.parse(path, true); }, function (_a) { | ||
var query = _a.query, pathname = _a.pathname; | ||
return ({ query: query, pathname: pathname }); | ||
}); | ||
var replacePathInParsedUrl = function (_a) { | ||
var getParsedPathFromString = function (maybePath) { | ||
return pipe_ts_1.pipeWith(maybePath, function (maybe) { return maybe_1.mapMaybe(maybe, parsePath); }, function (maybe) { return maybe_1.getOrElseMaybe(maybe, function () { return ({ query: null, pathname: null }); }); }); | ||
}; | ||
exports.replacePathInParsedUrl = function (_a) { | ||
var newPath = _a.newPath; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return pipe_1.pipe(function () { return parsePath(newPath); }, function (newPathParsed) { return (__assign({}, parsedUrl, newPathParsed)); })({}); | ||
return pipe_ts_1.pipeWith(newPath instanceof Function ? newPath(parsedUrl.pathname) : newPath, getParsedPathFromString, function (newPathParsed) { return (__assign({}, parsedUrl, newPathParsed)); }); | ||
}; | ||
}; | ||
exports.replacePathInUrl = other_1.flipCurried(pipe_1.pipe(replacePathInParsedUrl, mapUrl)); | ||
var replacePathnameInParsedUrl = function (_a) { | ||
exports.replacePathInUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.replacePathInParsedUrl, exports.mapUrl)); | ||
exports.replacePathnameInParsedUrl = function (_a) { | ||
var newPathname = _a.newPathname; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return (__assign({}, parsedUrl, { pathname: newPathname })); | ||
return (__assign({}, parsedUrl, { pathname: newPathname instanceof Function ? newPathname(parsedUrl.pathname) : newPathname })); | ||
}; | ||
}; | ||
exports.replacePathnameInUrl = other_1.flipCurried(pipe_1.pipe(replacePathnameInParsedUrl, mapUrl)); | ||
var appendPathnameToParsedUrl = function (_a) { | ||
exports.replacePathnameInUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.replacePathnameInParsedUrl, exports.mapUrl)); | ||
exports.appendPathnameToParsedUrl = function (_a) { | ||
var pathnameToAppend = _a.pathnameToAppend; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
var pathnameParts = pipe_1.pipe(function () { return maybe_1.mapMaybe(parsedUrl.pathname, getPartsFromPathname); }, function (maybe) { return maybe_1.getOrElseMaybe(maybe, function () { return []; }); })({}); | ||
var pathnamePartsToAppend = getPartsFromPathname(pathnameToAppend); | ||
var newPathnameParts = pathnameParts.concat(pathnamePartsToAppend); | ||
var newPathname = getPathnameFromParts(newPathnameParts); | ||
return __assign({}, parsedUrl, { pathname: newPathname }); | ||
}; | ||
return exports.replacePathnameInParsedUrl({ | ||
newPathname: function (prevPathname) { | ||
var pathnameParts = pipe_ts_1.pipeWith(maybe_1.mapMaybe(prevPathname, getPartsFromPathname), function (maybe) { | ||
return maybe_1.getOrElseMaybe(maybe, function () { return []; }); | ||
}); | ||
var pathnamePartsToAppend = getPartsFromPathname(pathnameToAppend); | ||
var newPathnameParts = pathnameParts.concat(pathnamePartsToAppend); | ||
var newPathname = getPathnameFromParts(newPathnameParts); | ||
return newPathname; | ||
}, | ||
}); | ||
}; | ||
exports.appendPathnameToUrl = other_1.flipCurried(pipe_1.pipe(appendPathnameToParsedUrl, mapUrl)); | ||
var replaceHashInParsedUrl = function (_a) { | ||
exports.appendPathnameToUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.appendPathnameToParsedUrl, exports.mapUrl)); | ||
exports.replaceHashInParsedUrl = function (_a) { | ||
var newHash = _a.newHash; | ||
return function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return (__assign({}, parsedUrl, { hash: newHash })); | ||
return (__assign({}, parsedUrl, { hash: newHash instanceof Function ? newHash(parsedUrl.hash) : newHash })); | ||
}; | ||
}; | ||
exports.replaceHashInUrl = other_1.flipCurried(pipe_1.pipe(replaceHashInParsedUrl, mapUrl)); | ||
exports.replaceHashInUrl = other_1.flipCurried(pipe_ts_1.pipe(exports.replaceHashInParsedUrl, exports.mapUrl)); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var assert = require("assert"); | ||
var pipe_ts_1 = require("pipe-ts"); | ||
var urlHelpers = require("url"); | ||
var index_1 = require("./index"); | ||
assert.strictEqual(index_1.addQueryToUrl({ url: 'http://foo.com/' })({ | ||
queryToAppend: { | ||
string: 'string', | ||
number: 1, | ||
boolean: true, | ||
strings: ['string1', 'string2'], | ||
}, | ||
}), 'http://foo.com/?string=string&number=1&boolean=true&strings=string1&strings=string2'); | ||
assert.deepEqual(pipe_ts_1.pipeWith(urlHelpers.parse('https://foo.com/bar', true), function (parsedUrl) { | ||
return index_1.mapParsedUrl(function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return (__assign({}, parsedUrl, { pathname: '/foo', query: { a: 'b' } })); | ||
})({ parsedUrl: parsedUrl }); | ||
}, urlHelpers.format), 'https://foo.com/foo?a=b'); | ||
assert.deepEqual(pipe_ts_1.pipeWith('https://foo.com/bar', function (url) { | ||
return index_1.mapUrl(function (_a) { | ||
var parsedUrl = _a.parsedUrl; | ||
return (__assign({}, parsedUrl, { pathname: '/foo', query: { a: 'b' } })); | ||
})({ url: url }); | ||
}), 'https://foo.com/foo?a=b'); | ||
assert.deepEqual(pipe_ts_1.pipeWith('https://foo.com/bar', function (url) { | ||
return index_1.mapUrl(pipe_ts_1.pipe(index_1.replacePathnameInParsedUrl({ newPathname: function () { return '/foo'; } }), function (parsedUrl) { | ||
return index_1.replaceQueryInParsedUrl({ newQuery: function () { return ({ a: 'b' }); } })({ parsedUrl: parsedUrl }); | ||
}))({ url: url }); | ||
}), 'https://foo.com/foo?a=b'); | ||
assert.strictEqual(index_1.replaceQueryInUrl({ | ||
url: '/foo?string=string&number=1&boolean=true&strings=string1&strings=string2', | ||
})({ | ||
newQuery: { foo: 1 }, | ||
}), '/foo?foo=1'); | ||
assert.strictEqual(index_1.replaceQueryInUrl({ | ||
url: 'http://foo.com/?string=string&number=1&boolean=true&strings=string1&strings=string2', | ||
@@ -23,2 +50,10 @@ })({ | ||
}), 'http://foo.com/'); | ||
assert.strictEqual(index_1.addQueryToUrl({ url: 'http://foo.com/' })({ | ||
queryToAppend: { | ||
string: 'string', | ||
number: 1, | ||
boolean: true, | ||
strings: ['string1', 'string2'], | ||
}, | ||
}), 'http://foo.com/?string=string&number=1&boolean=true&strings=string1&strings=string2'); | ||
assert.strictEqual(index_1.addQueryToUrl({ url: 'http://foo:bar@baz.com/' })({ | ||
@@ -31,10 +66,13 @@ queryToAppend: { a: 'b' }, | ||
assert.strictEqual(index_1.replacePathInUrl({ url: 'https://foo.com/foo?example' })({ newPath: '/bar' }), 'https://foo.com/bar'); | ||
assert.strictEqual(index_1.replacePathInUrl({ url: 'https://foo.com/foo?example' })({ newPath: null }), 'https://foo.com'); | ||
assert.strictEqual(index_1.replacePathnameInUrl({ url: 'https://foo.com/foo' })({ newPathname: '/bar' }), 'https://foo.com/bar'); | ||
assert.strictEqual(index_1.replacePathnameInUrl({ url: 'https://foo.com/foo?example' })({ newPathname: '/bar' }), 'https://foo.com/bar?example'); | ||
assert.strictEqual(index_1.replacePathnameInUrl({ url: 'https://foo.com/foo' })({ newPathname: null }), 'https://foo.com'); | ||
assert.strictEqual(index_1.replacePathnameInUrl({ url: 'https://foo.com/foo?example=' })({ newPathname: '/bar' }), 'https://foo.com/bar?example='); | ||
assert.strictEqual(index_1.appendPathnameToUrl({ url: '/foo' })({ pathnameToAppend: '/bar' }), '/foo/bar'); | ||
assert.strictEqual(index_1.appendPathnameToUrl({ url: '/foo?example' })({ pathnameToAppend: '/bar' }), '/foo/bar?example'); | ||
assert.strictEqual(index_1.appendPathnameToUrl({ url: '/foo/' })({ pathnameToAppend: '/bar' }), '/foo/bar'); | ||
assert.strictEqual(index_1.appendPathnameToUrl({ url: '/foo?example=' })({ pathnameToAppend: '/bar' }), '/foo/bar?example='); | ||
assert.strictEqual(index_1.appendPathnameToUrl({ url: '/@foo' })({ pathnameToAppend: '/bar' }), '/@foo/bar'); | ||
assert.strictEqual(index_1.replaceHashInUrl({ url: '/foo' })({ newHash: '#bar' }), '/foo#bar'); | ||
assert.strictEqual(index_1.replaceHashInUrl({ url: '/foo#bar' })({ newHash: undefined }), '/foo'); | ||
assert.strictEqual(index_1.replaceHashInUrl({ url: '/foo#bar' })({ newHash: null }), '/foo'); | ||
assert.strictEqual(index_1.replaceHashInUrl({ url: '/foo#bar' })({ newHash: '#baz' }), '/foo#baz'); | ||
//# sourceMappingURL=tests.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22981
273
2
3
14
1
+ Addedpipe-ts@^0.0.8
+ Added@types/node@12.20.55(transitive)
+ Addedpipe-ts@0.0.8(transitive)
- Removed@types/node@10.17.60(transitive)
Updated@types/node@^12.12.16