Skip to content

Commit

Permalink
fix: handle multiple constructor handlers built into Array
Browse files Browse the repository at this point in the history
  • Loading branch information
tale committed Mar 11, 2022
1 parent 55b9511 commit 8f876d1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,22 @@ export class Package extends BinaryControl implements IPackage {
* It will populate the strictly typed fields and also leave the raw-string value and key.
*/
export class Packages extends Array<Package> {
constructor(rawData: string) {
const cleanedData = rawData.replaceAll(/\r\n|\r|\n/g, '\n').replaceAll(/\0/g, '').normalize().trim()
const packageChunks = cleanedData.split('\n\n') // We know it will always be \n\n because of our cleanup
constructor(args: string) {
if (typeof args === 'string') {
const cleanedData = args.replaceAll(/\r\n|\r|\n/g, '\n').replaceAll(/\0/g, '').normalize().trim()
const packageChunks = cleanedData.split('\n\n') // We know it will always be \n\n because of our cleanup

const cleanedArray = packageChunks.map(chunk => {
if (chunk.trim().length > 0) {
return new Package(chunk)
}
}).filter(item => item) as Package[]
super(...cleanedArray)
const cleanedArray = packageChunks.map(chunk => {
if (chunk.trim().length > 0) {
return new Package(chunk)
}
}).filter(item => item) as Package[]
super(...cleanedArray)
} else {
// Arrays have multiple constructor handlers
// Let's not break the default behavior
const passer = args as any
super(passer)
}
}
}

0 comments on commit 8f876d1

Please sign in to comment.