Skip to content

Commit

Permalink
fix: correct param name typos
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Aug 15, 2024
1 parent f81a6be commit 96822af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
62 changes: 24 additions & 38 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ const { isObject } = require('./objects')
const { isNonEmptyString } = require('./strings')

const reusedSearchParams = new URLSearchParams()
const reusedSearchParamKey = '_'
const reusedSearchParamOffset = 2 // '_='.length

const { encodeURIComponent } = globalThis

function encodeWithColonAndForwardSlash(str) {
return encodeURIComponent(str).replace(/%3A/g, ':').replace(/%2F/g, '/')
}

function encodeWithColonAndPlusSign(str) {
return encodeURIComponent(str).replace(/%3A/g, ':').replace(/%2B/g, '+')
}

function encodeWithForwardSlash(str) {
return encodeURIComponent(str).replace(/%2F/g, '/')
}

function encodeNamespace(namespace) {
return isNonEmptyString(namespace)
? encodeWithColonAndForwardSlash(namespace)
? encodeURIComponent(namespace)
.replace(/%3A/g, ':')
.replace(/%2F/g, '/')
: ''
}

function encodeVersion(version) {
return isNonEmptyString(version) ? encodeWithColonAndPlusSign(version) : ''
function encodeQualifierParam(param) {
if (isNonEmptyString(param)) {
// Param key and value are encoded with `percentEncodeSet` of
// 'application/x-www-form-urlencoded' and `spaceAsPlus` of `true`.
// https://url.spec.whatwg.org/#urlencoded-serializing
reusedSearchParams.set(reusedSearchParamKey, param)
return replacePlusSignWithPercentEncodedSpace(
reusedSearchParams.toString().slice(reusedSearchParamOffset)
)
}
return ''
}

function encodeQualifiers(qualifiers) {
Expand All @@ -43,26 +44,16 @@ function encodeQualifiers(qualifiers) {
return ''
}

function encodeQualifierParam(qualifierValue) {
return isNonEmptyString
? encodeURLSearchParamWithPercentEncodedSpace(param)
: ''
}

function encodeSubpath(subpath) {
return isNonEmptyString(subpath) ? encodeWithForwardSlash(subpath) : ''
}

function encodeURLSearchParam(param) {
// Param key and value are encoded with `percentEncodeSet` of
// 'application/x-www-form-urlencoded' and `spaceAsPlus` of `true`.
// https://url.spec.whatwg.org/#urlencoded-serializing
reusedSearchParams.set('_', qualifierValue)
return reusedSearchParams.toString().slice(2)
return isNonEmptyString(subpath)
? encodeURIComponent(subpath).replace(/%2F/g, '/')
: ''
}

function encodeURLSearchParamWithPercentEncodedSpace(str) {
return replacePlusSignWithPercentEncodedSpace(encodeURLSearchParam(str))
function encodeVersion(version) {
return isNonEmptyString(version)
? encodeURIComponent(version).replace(/%3A/g, ':').replace(/%2B/g, '+')
: ''
}

function replacePlusSignWithPercentEncodedSpace(str) {
Expand All @@ -71,15 +62,10 @@ function replacePlusSignWithPercentEncodedSpace(str) {
}

module.exports = {
encodeWithColonAndForwardSlash,
encodeWithColonAndPlusSign,
encodeWithForwardSlash,
encodeNamespace,
encodeVersion,
encodeQualifiers,
encodeQualifierParam,
encodeSubpath,
encodeURIComponent,
encodeURLSearchParam,
encodeURLSearchParamWithPercentEncodedSpace
encodeURIComponent
}
20 changes: 10 additions & 10 deletions src/purl-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ const {
const { createHelpersNamespaceObject } = require('./helpers')

const {
normalizeName,
normalizeType,
normalizeNamespace,
normalizeName,
normalizeVersion,
normalizeQualifiers,
normalizeSubpath,
normalizeType,
normalizeVersion
normalizeSubpath
} = require('./normalize')

const { localeCompare } = require('./strings')
const { localeCompare, isNonEmptyString } = require('./strings')

const {
validateName,
validateType,
validateNamespace,
validateName,
validateVersion,
validateQualifiers,
validateQualifierKey,
validateSubpath,
validateType,
validateVersion
validateSubpath
} = require('./validate')

const PurlComponentEncoder = (comp) =>
typeof comp === 'string' && comp.length ? encodeURIComponent(comp) : ''
isNonEmptyString(comp) ? encodeURIComponent(comp) : ''

const PurlComponentStringNormalizer = (comp) =>
typeof comp === 'string' ? comp : undefined
Expand Down
3 changes: 2 additions & 1 deletion src/validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { isNullishOrEmptyString } = require('./lang')
const { isNonEmptyString } = require('./strings')

function validateEmptyByType(type, name, value, throws) {
if (!isNullishOrEmptyString(value)) {
Expand Down Expand Up @@ -104,7 +105,7 @@ function validateRequiredByType(type, name, value, throws) {
}

function validateStartsWithoutNumber(name, value, throws) {
if (value.length !== 0) {
if (isNonEmptyString(value)) {
const code = value.charCodeAt(0)
if (code >= 48 /*'0'*/ && code <= 57 /*'9'*/) {
if (throws) {
Expand Down

0 comments on commit 96822af

Please sign in to comment.