feat(pkg): Add core extensions package

This commit is contained in:
T. R. Bernstein
2025-04-21 01:46:55 +02:00
parent 7fd56d9e6c
commit 1ba1659600
6 changed files with 271 additions and 141 deletions

View File

@@ -0,0 +1,28 @@
{
"name": "@tabshift/core-extensions",
"description": "Extensions to JavaScript core objects",
"version": "1.0.0",
"type": "module",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"prepublish": "pnpm build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@tabshift/typescript-config": "workspace:",
"typescript": "latest"
},
"author": "T. R. Bernstein <ljspkgs01-project@tabshift.dev>",
"license": "EUPL-1.2",
"exports": {
"./object/map": {
"import": "./dist/object/map.js",
"types": "./dist/object/map.d.ts"
}
}
}

View File

@@ -0,0 +1,52 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const srcDir = path.resolve(__dirname, '../src')
const distDir = 'dist'
const pkgPath = path.resolve(__dirname, '../package.json')
function walk(dir) {
let result = []
const entries = fs.readdirSync(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
result = result.concat(walk(fullPath))
} else if (entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
result.push(fullPath)
}
}
return result
}
function toSubpath(filePath) {
const relativePath = path.relative(srcDir, filePath).replace(/\.ts$/, '')
return relativePath.replace(/\\/g, '/') // For Windows compatibility
}
function updatePackageJson() {
const packageJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
const files = walk(srcDir)
const exportsMap = {}
files.forEach((file) => {
const subpath = toSubpath(file)
exportsMap[`./${subpath}`] = {
import: `./${distDir}/${subpath}.js`,
types: `./${distDir}/${subpath}.d.ts`
}
})
packageJson.exports = exportsMap
fs.writeFileSync(pkgPath, JSON.stringify(packageJson, null, 2))
console.log('✅ Modern exports updated in package.json')
}
updatePackageJson()

View File

@@ -0,0 +1,21 @@
declare global {
interface Object {
map<T, U>(
this: Record<string, T>,
callback: (value: T, key: string, obj: Record<string, T>) => U
): Record<string, U>
}
}
Object.prototype.map = function <T, U>(
this: Record<string, T>,
callback: (value: T, key: string, obj: Record<string, T>) => U
): Record<string, U> {
const result: Record<string, U> = {}
for (const [key, value] of Object.entries(this)) {
result[key] = callback(value, key, this)
}
return result
}
export {}

View File

@@ -0,0 +1,10 @@
{
"extends": "@tabshift/typescript-config/base.json",
"compilerOptions": {
"declaration": true,
"declarationDir": "dist",
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}