Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Allow for n-darray (#144)
Browse files Browse the repository at this point in the history
* Allow for n-darray

* changeset

* format
  • Loading branch information
mateuszradomski authored Mar 4, 2024
1 parent 975ac73 commit 858026e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
6 changes: 6 additions & 0 deletions packages/discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @l2beat/discovery

## 0.44.1

### Patch Changes

- Allow for n-darray in normalizeDiffPath

## 0.44.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@l2beat/discovery",
"description": "L2Beat discovery - engine & tooling utilized for keeping an eye on L2s",
"version": "0.44.0",
"version": "0.44.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand Down
32 changes: 20 additions & 12 deletions packages/discovery/src/discovery/utils/normalizeDiffPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ describe(normalizeDiffPath.name, () => {
expect(normalizeDiffPath('test')).toEqual('test')
})

it('should remove array suffix and values.prefix', () => {
it('remove array suffix and prefixes', () => {
expect(normalizeDiffPath('values.test.0')).toEqual('test')
expect(normalizeDiffPath('values.test.1')).toEqual('test')
expect(normalizeDiffPath('values.test.12')).toEqual('test')
expect(normalizeDiffPath('values.test.01')).toEqual('test')
expect(normalizeDiffPath('values.test.21')).toEqual('test')
expect(normalizeDiffPath('values.test.0.1')).toEqual('test')
expect(normalizeDiffPath('values.test.2.1.0')).toEqual('test')

expect(normalizeDiffPath('upgradeability.test.0')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.1')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.12')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.01')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.21')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.0.1')).toEqual('test')
expect(normalizeDiffPath('upgradeability.test.2.1.0')).toEqual('test')
})

it('should remove array suffix when no values. prefix', () => {
it('remove array suffix when no values. prefix', () => {
expect(normalizeDiffPath('test.0')).toEqual('test')
expect(normalizeDiffPath('test.1')).toEqual('test')
expect(normalizeDiffPath('test.12')).toEqual('test')
Expand All @@ -39,39 +43,43 @@ describe(normalizeDiffPath.name, () => {

it('should throw on different prefix', () => {
expect(() => normalizeDiffPath('test.values.0')).toThrow(
'Expected test.values.0 to have only one suffix',
'Expected test.values.0 to have only numeric suffixes',
)
})
})

describe(removeArraySuffix.name, () => {
it('should remove the array suffix', () => {
it('remove single array suffix', () => {
expect(removeArraySuffix('test.0')).toEqual('test')
expect(removeArraySuffix('test.1')).toEqual('test')
expect(removeArraySuffix('test.12')).toEqual('test')
expect(removeArraySuffix('test.01')).toEqual('test')
expect(removeArraySuffix('test.21')).toEqual('test')
})

it('remove multiple array suffixes', () => {
expect(removeArraySuffix('test.0.0')).toEqual('test')
expect(removeArraySuffix('test.0.6')).toEqual('test')
expect(removeArraySuffix('test.0.6.0')).toEqual('test')
expect(removeArraySuffix('test.1.2.3.4.5.6.7')).toEqual('test')
})

it('should return the input if it does not contain a suffix', () => {
expect(removeArraySuffix('test')).toEqual('test')
})

it('should throw if the suffix is not a decimal number', () => {
expect(() => removeArraySuffix('test.a')).toThrow(
'Expected a to be a number',
'Expected test.a to have only numeric suffixes',
)
expect(() => removeArraySuffix('test.1a')).toThrow(
'Expected 1a to be a number',
'Expected test.1a to have only numeric suffixes',
)
expect(() => removeArraySuffix('test.1e2')).toThrow(
'Expected 1e2 to be a number',
'Expected test.1e2 to have only numeric suffixes',
)
})

it('should throw if the input has more than one suffix', () => {
expect(() => removeArraySuffix('test.1.2')).toThrow(
'Expected test.1.2 to have only one suffix',
expect(() => removeArraySuffix('test.0.')).toThrow(
'Expected test.0. to have only numeric suffixes',
)
})
})
8 changes: 4 additions & 4 deletions packages/discovery/src/discovery/utils/normalizeDiffPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export function removeArraySuffix(path: string): string {
if (path.includes('.')) {
const [name, ...rest] = path.split('.')

assert(rest.length === 1, `Expected ${path} to have only one suffix`)
assert(rest.length >= 1, `Unreachable code`)
assert(name !== undefined, `Unexpected undefined value`)
assert(
name !== undefined && rest[0] !== undefined,
`Unexpected undefined value`,
rest.every((p) => p.length > 0 && isIntNumeric(p)),
`Expected ${path} to have only numeric suffixes`,
)
assert(isIntNumeric(rest[0]), `Expected ${rest[0]} to be a number`)
return name
}

Expand Down

0 comments on commit 858026e

Please sign in to comment.