@yuheiy/format-url-query
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { ParsedUrlQuery } from 'querystring' | ||
export default function formatUrlQuery( | ||
export default function formatUrlQuery<T extends ParsedUrlQuery>( | ||
parsedUrlQuery: ParsedUrlQuery, | ||
format: { | ||
[key: string]: string | ||
}, | ||
): { | ||
[key: string]: string | ||
} | ||
export default function formatUrlQuery( | ||
parsedUrlQuery: ParsedUrlQuery, | ||
format: { | ||
[key: string]: string[] | ||
}, | ||
): { | ||
[key: string]: string[] | ||
} | ||
export default function formatUrlQuery( | ||
parsedUrlQuery: ParsedUrlQuery, | ||
format: { | ||
[key: string]: string | string[] | ||
}, | ||
): { | ||
[key: string]: string | string[] | ||
} | ||
format: T, | ||
): T |
35
index.js
@@ -24,33 +24,40 @@ 'use strict' | ||
// Shallow copy if type of `value` is array. | ||
const cloneIfArray = (value) => { | ||
return Array.isArray(value) ? [...value] : value | ||
} | ||
// Format the parsed URL query with default values. | ||
const formatUrlQuery = (parsedUrlQuery, format) => { | ||
if (!isValidQuery(parsedUrlQuery)) { | ||
throw new TypeError('parsedUrlQuery') // todo | ||
throw new TypeError('parsedUrlQuery is not a valid object.') | ||
} | ||
if (!isValidQuery(format)) { | ||
throw new TypeError('format') // todo | ||
throw new TypeError('format is not a valid object.') | ||
} | ||
return Object.keys(format).reduce((formatted, key) => { | ||
const defaultValue = format[key].slice() | ||
const value = (parsedUrlQuery[key] || '').slice() | ||
const expectedType = Array.isArray(defaultValue) ? 'array' : 'string' | ||
const actualType = Array.isArray(value) ? 'array' : 'string' | ||
const defaultValue = cloneIfArray(format[key]) | ||
const actualValue = cloneIfArray(parsedUrlQuery[key] || '') | ||
const isExpectedTypeArray = Array.isArray(defaultValue) | ||
const isExpectedTypeString = !isExpectedTypeArray | ||
const isActualTypeArray = Array.isArray(actualValue) | ||
const isActualTypeString = !isActualTypeArray | ||
const assignedValue = (() => { | ||
if (expectedType === 'string' && actualType === 'string') { | ||
return value || defaultValue | ||
if (isExpectedTypeString && isActualTypeString) { | ||
return actualValue || defaultValue | ||
} | ||
if (expectedType === 'string' && actualType === 'array') { | ||
return value[0] || defaultValue | ||
if (isExpectedTypeString && isActualTypeArray) { | ||
return actualValue[0] || defaultValue | ||
} | ||
if (expectedType === 'array' && actualType === 'string') { | ||
return value ? [value] : [] | ||
if (isExpectedTypeArray && isActualTypeString) { | ||
return actualValue ? [actualValue] : defaultValue | ||
} | ||
if (expectedType === 'array' && actualType === 'array') { | ||
return value.length ? value : defaultValue | ||
if (isExpectedTypeArray && isActualTypeArray) { | ||
return actualValue.length ? actualValue : defaultValue | ||
} | ||
@@ -57,0 +64,0 @@ })() |
{ | ||
"name": "@yuheiy/format-url-query", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Format the parsed URL query with default values.", | ||
@@ -24,2 +24,5 @@ "main": "index.js", | ||
"homepage": "https://github.com/yuheiy/format-url-query#readme", | ||
"dependencies": { | ||
"@types/node": "^11.9.0" | ||
}, | ||
"devDependencies": { | ||
@@ -33,3 +36,8 @@ "ava": "^1.2.1", | ||
"access": "public" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
} | ||
} |
@@ -66,2 +66,4 @@ # format-url-query [![Build Status](https://travis-ci.com/yuheiy/format-url-query.svg?branch=master)](https://travis-ci.com/yuheiy/format-url-query) | ||
Returns an object of the same type as the `format`. | ||
#### parsedUrlQuery | ||
@@ -68,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5498
83
1
62
+ Added@types/node@^11.9.0
+ Added@types/node@11.15.54(transitive)