Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@yuheiy/format-url-query

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yuheiy/format-url-query - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

28

index.d.ts
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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc