sort-package-json
Advanced tools
Comparing version 2.10.0 to 2.10.1
62
index.js
@@ -58,19 +58,20 @@ import sortObjectKeys from 'sort-object-keys' | ||
const parseNameAndVersionRange = (specifier) => { | ||
// Ignore anything after > & rely on fallback alphanumeric sorting for that | ||
const [nameAndVersion] = specifier.split('>') | ||
const atMatches = [...nameAndVersion.matchAll('@')] | ||
if ( | ||
!atMatches.length || | ||
(atMatches.length === 1 && atMatches[0].index === 0) | ||
) { | ||
return { name: specifier } | ||
} | ||
const splitIndex = atMatches.pop().index | ||
return { | ||
name: nameAndVersion.substring(0, splitIndex), | ||
range: nameAndVersion.substring(splitIndex + 1), | ||
} | ||
} | ||
const sortObjectBySemver = sortObjectBy((a, b) => { | ||
const parseNameAndVersionRange = (specifier) => { | ||
// Ignore anything after > & rely on fallback alphanumeric sorting for that | ||
const [nameAndVersion] = specifier.split('>') | ||
const atMatches = [...nameAndVersion.matchAll('@')] | ||
if ( | ||
!atMatches.length || | ||
(atMatches.length === 1 && atMatches[0].index === 0) | ||
) { | ||
return { name: specifier } | ||
} | ||
const splitIndex = atMatches.pop().index | ||
return { | ||
name: nameAndVersion.substring(0, splitIndex), | ||
range: nameAndVersion.substring(splitIndex + 1), | ||
} | ||
} | ||
const { name: aName, range: aRange } = parseNameAndVersionRange(a) | ||
@@ -91,2 +92,23 @@ const { name: bName, range: bRange } = parseNameAndVersionRange(b) | ||
const getPackageName = (ident) => { | ||
const parts = ident.split('@') | ||
if (ident.startsWith('@')) { | ||
// Handle cases where package name starts with '@' | ||
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident | ||
} | ||
// Handle cases where package name doesn't start with '@' | ||
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident | ||
} | ||
const sortObjectByIdent = (a, b) => { | ||
const PackageNameA = getPackageName(a) | ||
const PackageNameB = getPackageName(b) | ||
if (PackageNameA < PackageNameB) return -1 | ||
if (PackageNameA > PackageNameB) return 1 | ||
return 0 | ||
} | ||
// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js | ||
@@ -346,3 +368,3 @@ const eslintBaseConfigProperties = [ | ||
{ key: 'devDependencies', over: sortObject }, | ||
{ key: 'dependenciesMeta', over: sortObjectBy(undefined, true) }, | ||
{ key: 'dependenciesMeta', over: sortObjectBy(sortObjectByIdent, true) }, | ||
{ key: 'peerDependencies', over: sortObject }, | ||
@@ -386,3 +408,3 @@ // TODO: only sort depth = 2 | ||
if (typeof json === 'string') { | ||
const { indent } = detectIndent(json) | ||
const { indent, type } = detectIndent(json) | ||
const endCharacters = json.slice(-1) === '\n' ? '\n' : '' | ||
@@ -392,3 +414,5 @@ const newline = detectNewline(json) | ||
let result = JSON.stringify(over(json), null, indent) + endCharacters | ||
let result = | ||
JSON.stringify(over(json), null, type === 'tab' ? '\t' : indent) + | ||
endCharacters | ||
if (newline === '\r\n') { | ||
@@ -395,0 +419,0 @@ result = result.replace(/\n/g, newline) |
{ | ||
"name": "sort-package-json", | ||
"version": "2.10.0", | ||
"version": "2.10.1", | ||
"description": "Sort an Object or package.json based on the well-known package.json keys", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
33338
7
635