{"name":"@eggjs/tegg-common-util","version":"3.78.2","keywords":["egg","typescript","collection","tegg"],"author":{"name":"killagu","email":"killa123@126.com"},"license":"MIT","_id":"@eggjs/tegg-common-util@3.78.2","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":"hyj1991","email":"yeekwanvong@gmail.com"},{"name":"killagu","email":"killa07071201@gmail.com"},{"name":"coolme200","email":"2001-wms@163.com"},{"name":"akitasummer","email":"644171127@qq.com"},{"name":"niania","email":"ljracm@gmail.com"}],"homepage":"https://github.com/eggjs/tegg","bugs":{"url":"https://github.com/eggjs/tegg/issues"},"dist":{"shasum":"fafb1c02ff98753246185236f01cafa01a970e78","tarball":"http://registry-npm.ywwl.com/@eggjs/tegg-common-util/download/@eggjs/tegg-common-util-3.78.2.tgz","fileCount":27,"integrity":"sha512-SmI7D9soYCoCfPxs9KyLFqhGUOby48XOFB7sMuuTkZPdFAHCx9S+4OwGIL7H9Dqwekhs6YonvXjt89SaY1X18A==","signatures":[{"sig":"MEQCIDmy4rWa9g1dZMe6KsePZZj4TjA48cSzPm7id4xgirsyAiBXnIlmOql6qKBWci+t/SDu+KnK7wowZFcbrWJtr9wkww==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":74509,"size":19590,"noattachment":false,"key":"/@eggjs/tegg-common-util/-/@eggjs/tegg-common-util-3.78.2.tgz"},"main":"dist/index.js","engines":{"node":">=14.0.0"},"gitHead":"900e4ae13deec293bc5c4f1f698d9989bc40f94d","scripts":{"tsc":"ut run clean && tsc -p ./tsconfig.json","test":"cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha","clean":"tsc -b --clean","tsc:pub":"ut run clean && tsc -p ./tsconfig.pub.json","prepublishOnly":"ut tsc:pub"},"typings":"dist/index.d.ts","_npmUser":{"name":"niania","email":"ljracm@gmail.com"},"repository":{"url":"git+ssh://git@github.com/eggjs/tegg.git","type":"git","directory":"core/common-util"},"_npmVersion":"lerna/6.6.2/node@v20.20.2+x64 (linux)","description":"common util for tegg","directories":{},"_nodeVersion":"20.20.2","dependencies":{"globby":"^11.1.0","extend2":"^1.0.0","js-yaml":"^3.14.0","@eggjs/tegg-types":"^3.78.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^10.2.0","ts-node":"^10.9.1","cross-env":"^7.0.3","typescript":"^5.0.4","@types/node":"^20.2.4","@types/mocha":"^10.0.1"},"_npmOperationalInternal":{"tmp":"tmp/tegg-common-util_3.78.2_1775748101643_0.6946701660173795","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":1775748101772,"_cnpm_publish_time":1775748101772,"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"}}