Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions packages/normalize-package-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# @simple-libs/normalize-package-data

[![ESM-only package][package]][package-url]
[![NPM version][npm]][npm-url]
[![Node version][node]][node-url]
[![Dependencies status][deps]][deps-url]
[![Install size][size]][size-url]
[![Build status][build]][build-url]
[![Coverage status][coverage]][coverage-url]

[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
[package-url]: https://nodejs.org/api/esm.html

[npm]: https://img.shields.io/npm/v/@simple-libs/normalize-package-data.svg
[npm-url]: https://www.npmjs.com/package/@simple-libs/normalize-package-data

[node]: https://img.shields.io/node/v/@simple-libs/normalize-package-data.svg
[node-url]: https://nodejs.org

[deps]: https://img.shields.io/librariesio/release/npm/@simple-libs/normalize-package-data
[deps-url]: https://libraries.io/npm/@simple-libs%2Fnormalize-package-data

[size]: https://packagephobia.com/badge?p=@simple-libs/normalize-package-data
[size-url]: https://packagephobia.com/result?p=@simple-libs/normalize-package-data

[build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/simple-libs/tests.yml?branch=main
[build-url]: https://github.com/TrigenSoftware/simple-libs/actions

[coverage]: https://coveralls.io/repos/github/TrigenSoftware/simple-libs/badge.svg?branch=main
[coverage-url]: https://coveralls.io/github/TrigenSoftware/simple-libs?branch=main

A small library to normalize package data.

## Install

```bash
# pnpm
pnpm add @simple-libs/normalize-package-data
# yarn
yarn add @simple-libs/normalize-package-data
# npm
npm i @simple-libs/normalize-package-data
```

## Usage

```ts
import { normalizePackageData } from '@simple-libs/normalize-package-data'

normalizePackageData({
name: ' package ',
version: 'v1.2.3',
repository: 'conventional-changelog/conventional-changelog'
})
/* {
name: 'package',
version: '1.2.3',
repository: {
type: 'git',
url: 'https://github.com/conventional-changelog/conventional-changelog'
},
bugs: {
url: 'https://github.com/conventional-changelog/conventional-changelog/issues'
},
homepage: 'https://github.com/conventional-changelog/conventional-changelog#readme'
} */

normalizePackageData({
bugs: 'support@example.com',
homepage: 'example.com'
})
/* {
name: '',
version: '',
bugs: {
email: 'support@example.com'
},
homepage: 'http://example.com'
} */
```
15 changes: 15 additions & 0 deletions packages/normalize-package-data/oxlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from '@trigen/oxlint'
import testConfig from '@trigen/oxlint-config/test'
import tsTypeCheckedConfig from '@trigen/oxlint-config/typescript-type-checked'
import rootConfig from '../../oxlint.config.ts'

export default defineConfig({
extends: [
rootConfig,
tsTypeCheckedConfig,
testConfig
],
rules: {
'typescript/no-explicit-any': 'off'
}
})
60 changes: 60 additions & 0 deletions packages/normalize-package-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@simple-libs/normalize-package-data",
"type": "module",
"version": "1.0.0",
"description": "A small library to normalize package data.",
"author": {
"name": "Dan Onoshko",
"email": "danon0404@gmail.com",
"url": "https://github.com/dangreen"
},
"license": "MIT",
"homepage": "https://github.com/TrigenSoftware/simple-libs/tree/main/packages/normalize-package-data#readme",
"funding": "https://ko-fi.com/dangreen",
"repository": {
"type": "git",
"url": "https://github.com/TrigenSoftware/simple-libs.git",
"directory": "packages/normalize-package-data"
},
"bugs": {
"url": "https://github.com/TrigenSoftware/simple-libs/issues"
},
"keywords": [
"package",
"normalize"
],
"engines": {
"node": ">=22"
},
"exports": "./src/index.ts",
"publishConfig": {
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"directory": "package",
"linkDirectory": false
},
"files": [
"dist"
],
"scripts": {
"clear:package": "del ./package",
"clear:dist": "del ./dist",
"clear": "del ./package ./dist ./coverage",
"prepublishOnly": "run build clear:package clean-publish",
"postpublish": "pnpm clear:package",
"build": "tsgo -p tsconfig.build.json",
"lint": "oxlint",
"test:unit": "vitest run --coverage",
"test:types": "tsgo --noEmit",
"test": "run -p lint test:unit test:types"
},
"dependencies": {
"@simple-libs/hosted-git-info": "workspace:^",
"semver": "^7.8.5"
},
"devDependencies": {
"@types/semver": "^7.7.1"
}
}
2 changes: 2 additions & 0 deletions packages/normalize-package-data/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from './types.js'
export * from './normalize.js'
121 changes: 121 additions & 0 deletions packages/normalize-package-data/src/normalize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
describe,
expect,
it
} from 'vitest'
import { normalizePackageData } from './normalize.js'
import type { PackageData } from './types.js'

describe('normalize-package-data', () => {
it('should not mutate package data', () => {
const pkg: PackageData = {
name: ' package ',
version: 'v1.2.3',
repository: {
type: 'git',
url: 'git+https://github.com/conventional-changelog/conventional-changelog.git'
},
bugs: 'https://github.com/conventional-changelog/conventional-changelog/issues',
homepage: 'example.com'
}
const normalized = normalizePackageData(pkg)

expect(normalized).not.toBe(pkg)
expect(normalized.repository).not.toBe(pkg.repository)
expect(pkg).toEqual({
name: ' package ',
version: 'v1.2.3',
repository: {
type: 'git',
url: 'git+https://github.com/conventional-changelog/conventional-changelog.git'
},
bugs: 'https://github.com/conventional-changelog/conventional-changelog/issues',
homepage: 'example.com'
})
})

it('should normalize package name', () => {
const pkg = {
name: ' package '
}
const normalized = normalizePackageData(pkg)

expect(normalized.name).toBe('package')
})

it('should normalize package version', () => {
const pkg = {
version: 'v1.2.3'
}
const normalized = normalizePackageData(pkg)

expect(normalized.version).toBe('1.2.3')
})

it('should normalize string repository', () => {
const pkg: PackageData = {
repository: 'conventional-changelog/conventional-changelog'
}
const normalized = normalizePackageData(pkg)

expect(normalized.repository).toEqual({
type: 'git',
url: 'https://github.com/conventional-changelog/conventional-changelog'
})
})

it('should normalize repository url', () => {
const pkg: PackageData = {
repository: {
type: 'git',
url: 'git+https://github.com/conventional-changelog/conventional-changelog.git'
}
}
const normalized = normalizePackageData(pkg)

expect(normalized.repository).toEqual({
type: 'git',
url: 'https://github.com/conventional-changelog/conventional-changelog'
})
})

it('should normalize bugs from a hosted repository', () => {
const pkg: PackageData = {
repository: 'conventional-changelog/conventional-changelog'
}
const normalized = normalizePackageData(pkg)

expect(normalized.bugs).toEqual({
url: 'https://github.com/conventional-changelog/conventional-changelog/issues'
})
})

it('should normalize string bugs', () => {
const pkg: PackageData = {
bugs: 'https://github.com/conventional-changelog/conventional-changelog/issues'
}
const normalized = normalizePackageData(pkg)

expect(normalized.bugs).toEqual({
url: 'https://github.com/conventional-changelog/conventional-changelog/issues'
})
})

it('should normalize homepage from a hosted repository', () => {
const pkg: PackageData = {
repository: 'conventional-changelog/conventional-changelog'
}
const normalized = normalizePackageData(pkg)

expect(normalized.homepage).toBe('https://github.com/conventional-changelog/conventional-changelog#readme')
})

it('should normalize homepage protocol', () => {
const pkg = {
homepage: 'example.com'
}
const normalized = normalizePackageData(pkg)

expect(normalized.homepage).toBe('http://example.com')
})
})
Loading