{"name":"@eggjs/tegg-common-util","version":"4.0.0-beta.2","keywords":["egg","typescript","collection","tegg"],"author":{"name":"killagu","email":"killa123@126.com"},"license":"MIT","_id":"@eggjs/tegg-common-util@4.0.0-beta.2","homepage":"https://github.com/eggjs/tegg","bugs":{"url":"https://github.com/eggjs/tegg/issues"},"dist":{"shasum":"45b6ca2e12b685beeaa08d8c1d3b587dabc3910f","tarball":"http://registry-npm.ywwl.com/@eggjs/tegg-common-util/download/@eggjs/tegg-common-util-4.0.0-beta.2.tgz","fileCount":25,"integrity":"sha512-usDxp1ONVsV8aaPN3JShUzmKuIBpSmsZ6u6ItzgyAMFwHe0lXZ+dHUl7yCz/zRUFWlAwb0aToEZiRvb2w8WnOQ==","signatures":[{"sig":"MEUCIEgpx3aMK8tuN7HPOSnqzCpm355NQqVdzsfnKwd/S46cAiEA4UByNYXsF/r6L2T0M3pViYvZO1JYGcGZhAr7TAnssK4=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":56544,"size":14114,"noattachment":false,"key":"/@eggjs/tegg-common-util/-/@eggjs/tegg-common-util-4.0.0-beta.2.tgz"},"type":"module","engines":{"node":">=20.0.0"},"exports":{".":{"types":"./src/index.d.ts","default":"./src/index.js"}},"gitHead":"7e29d49dcc2eb26909697d5543b3c4f596c9e927","scripts":{"tsc":"npm run clean && tsc -p ./tsconfig.json","clean":"tsc -b --clean","tsc:pub":"npm run tsc","prepublishOnly":"npm run tsc"},"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"repository":{"url":"git+ssh://git@github.com/eggjs/tegg.git","type":"git","directory":"core/common-util"},"_npmVersion":"lerna/6.6.2/node@v22.14.0+arm64 (darwin)","description":"common util for tegg","directories":{},"_nodeVersion":"22.14.0","dependencies":{"globby":"^14.1.0","extend2":"^4.0.0","js-yaml":"^4.1.0","@eggjs/utils":"^4.3.0","@eggjs/tegg-types":"^4.0.0-beta.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"ts-node":"10","typescript":"5","@types/node":"22","@types/js-yaml":"^4.0.9"},"_npmOperationalInternal":{"tmp":"tmp/tegg-common-util_4.0.0-beta.2_1741012740862_0.9179527411286497","host":"s3://npm-registry-packages-npm-production"},"readme":"# @eggjs/tegg-common-util\n\nCommon utility functions for tegg framework.\n\n## ModuleConfigUtil.deduplicateModules\n\nA utility method for deduplicating module references to avoid adding duplicate modules.\n\n### Features\n\n- **Path-based deduplication**: Removes modules with duplicate paths\n- **Name-based deduplication**: Removes modules with duplicate names (throws error if found)\n- **Priority handling**: Prioritizes non-optional modules over optional ones\n- **Error handling**: Throws error for duplicate module names with different paths\n- **Simple API**: No complex options, straightforward behavior\n\n### API\n\n```typescript\npublic static deduplicateModules(\n  moduleReferences: readonly ModuleReference[]\n): readonly ModuleReference[]\n```\n\n**Note**: This method does not accept options parameters. The behavior is fixed and optimized for common use cases.\n\n### Behavior\n\n1. **Path Deduplication**: If multiple modules have the same path, only one is kept\n2. **Optional Priority**: When paths are the same, non-optional modules are prioritized over optional ones\n3. **Name Validation**: If modules have the same name but different paths, an error is thrown\n4. **First Occurrence**: When both modules have the same optional status, the first occurrence is kept\n\n### Usage Examples\n\n#### Basic Usage\n\n```typescript\nimport { ModuleConfigUtil } from '@eggjs/tegg-common-util';\n\nconst modules = [\n  { name: 'module1', path: '/path/to/module1' },\n  { name: 'module2', path: '/path/to/module2' },\n  { name: 'module1', path: '/different/path/to/module1' }, // Will throw error\n];\n\nconst result = ModuleConfigUtil.deduplicateModules(modules);\n// Throws Error: Duplicate module name \"module1\" found\n```\n\n#### With Optional Modules\n\n```typescript\nconst modules = [\n  { name: 'module1', path: '/path/to/module1', optional: true },\n  { name: 'module1', path: '/path/to/module1' }, // Non-optional version\n];\n\nconst result = ModuleConfigUtil.deduplicateModules(modules);\n// Result: 1 module, non-optional version kept\n```\n\n#### Path Deduplication\n\n```typescript\nconst modules = [\n  { name: 'module1', path: '/path/to/module1' },\n  { name: 'module2', path: '/path/to/module1' }, // Same path, different name\n];\n\nconst result = ModuleConfigUtil.deduplicateModules(modules);\n// Result: 1 module, first occurrence kept\n```\n\n### Use Cases\n\n#### 1. Plugin/Config Module Scanner\n\n```typescript\n// In ModuleScanner.loadModuleReferences()\nreturn ModuleConfigUtil.deduplicateModules(allModuleReferences);\n// Automatically handles deduplication with optimal defaults\n```\n\n#### 2. Standalone Runner\n\n```typescript\n// In Runner.getModuleReferences()\nreturn ModuleConfigUtil.deduplicateModules(allModuleReferences);\n// Simple and reliable deduplication\n```\n\n#### 3. Error Handling\n\n```typescript\ntry {\n  const result = ModuleConfigUtil.deduplicateModules(modules);\n} catch (error) {\n  if (error.message.includes('Duplicate module name')) {\n    // Handle duplicate module name error\n    console.error('Configuration error:', error.message);\n  }\n  throw error;\n}\n```\n\n### Benefits\n\n1. **Simplicity**: No complex configuration needed, works out of the box\n2. **Reliability**: Consistent behavior across different use cases\n3. **Performance**: Optimized for common scenarios\n4. **Error Prevention**: Catches configuration errors early\n5. **Maintainability**: Simple API reduces complexity\n\n### Migration from Options-based API\n\n#### Before (Options-based - Not Available)\n```typescript\n// This API never existed in the actual implementation\nconst result = ModuleConfigUtil.deduplicateModules(modules, {\n  prioritizeNonOptional: true,\n  allowNameDuplicates: false,\n  logPrefix: '[tegg/config]',\n  logger: customLogger,\n});\n```\n\n#### After (Current Implementation)\n```typescript\n// Current implementation - simple and direct\nconst result = ModuleConfigUtil.deduplicateModules(modules);\n// Automatically handles all deduplication logic\n```\n\n### Important Notes\n\n- **No Options**: The method signature is fixed and does not accept configuration options\n- **Error on Name Duplicates**: Duplicate names with different paths will cause an error\n- **Automatic Priority**: Non-optional modules are automatically prioritized over optional ones\n- **Path-based Deduplication**: Same path modules are deduplicated with smart priority handling\n","publish_time":1741012741934,"maintainers":[{"name":"gxkl","email":"gxkl203@gmail.com"},{"name":"eggjs-admin","email":"fengmk2+eggjs@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"atian25","email":"atian25@qq.com"},{"name":"dead_horse","email":"dead_horse@qq.com"},{"name":"wanghx","email":"whxaxes@gmail.com"},{"name":"hyj1991","email":"yeekwanvong@gmail.com"},{"name":"killagu","email":"killa07071201@gmail.com"},{"name":"coolme200","email":"2001-wms@163.com"},{"name":"mansonchor.zzw","email":"mansonchor1987@gmail.com"},{"name":"hubcarl","email":"hubcarl@126.com"},{"name":"gemwuu","email":"gemwuu@gmail.com"},{"name":"akitasummer","email":"644171127@qq.com"}],"_cnpm_publish_time":1741012741934,"dist-tags":{"alpha":"3.57.15-alpha.0","beta":"4.0.2-beta.27","beta-v3":"3.74.0-beta.3","graph-beta":"3.88.2-beta.0","latest":"4.1.0","rc":"4.0.2-rc.1"}}