feat(pkg): Add core extensions package
This commit is contained in:
28
packages/core-extensions/package.json
Normal file
28
packages/core-extensions/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
52
packages/core-extensions/scripts/generate-exports.js
Normal file
52
packages/core-extensions/scripts/generate-exports.js
Normal 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()
|
||||
21
packages/core-extensions/src/object/map.ts
Normal file
21
packages/core-extensions/src/object/map.ts
Normal 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 {}
|
||||
10
packages/core-extensions/tsconfig.json
Normal file
10
packages/core-extensions/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@tabshift/typescript-config/base.json",
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"declarationDir": "dist",
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user