Skip to content

Commit

Permalink
Fix KV3 pure numeric key
Browse files Browse the repository at this point in the history
  • Loading branch information
robincodex committed Sep 11, 2024
1 parent d0c4acd commit 83c07bc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions __tests__/KeyValues3.save.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,8 @@ Second line of a multi-line string literal.
r1 = deferred_resource:""
r2 = resource:""
}
"1" =
{
a = 1
}
}
11 changes: 9 additions & 2 deletions __tests__/KeyValues3.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test } from '@jest/globals';
import crypto from 'crypto';
import { join } from 'path';
import { KV3BaseValue } from '../src/KeyValues3';
import { KeyValues3, getKeyValuesAdapter } from '../src/node';
import { describe, expect, test } from '@jest/globals';
import crypto from 'crypto';

describe('KeyValues3', () => {
test('Check KeyValues3 Methods', () => {
Expand Down Expand Up @@ -303,12 +303,19 @@ Second line of a multi-line string literal.
}

const obj = root.FindKey('objectValue')?.GetValue();
expect(obj?.IsObject()).toBe(true);
if (obj?.IsObject()) {
expect(obj.FindKey('n')?.GetValue().Value()).toBe(5);
expect(obj.FindKey('s')?.GetValue().Value()).toBe('foo');
expect(obj.FindKey('h')?.GetValue().IsArray()).toBe(true);
}

const obj1 = root.FindKey('1')?.GetValue();
expect(obj1?.IsObject()).toBe(true);
if (obj1?.IsObject()) {
expect(obj1.FindKey('a')?.GetValue().Value()).toBe(1);
}

expect(
root
.FindKey('objectValue')
Expand Down
6 changes: 5 additions & 1 deletion __tests__/KeyValues3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,9 @@ Second line of a multi-line string literal.
m = .2-1
r1 = deferred_resource:""
r2 = resource:""
}
},
"1" =
{
a = 1
}
}
4 changes: 4 additions & 0 deletions __tests__/__snapshots__/KeyValues3.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,9 @@ Second line of a multi-line string literal.
r1 = deferred_resource:""
r2 = resource:""
}
"1" =
{
a = 1
}
}"
`;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-keyvalues",
"version": "1.2.4",
"version": "1.2.5",
"description": "Parse Valve KeyValues Format and easy to use in nodejs or browser.",
"main": "dist/node.js",
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions src/KeyValues3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ class ValueFeatureObject extends ValueObject {
}
}

const MatchKeyNoQuote = /^[\w\d_\.]+$/;
const MatchKeyNoQuote = /^[0-9a-zA-Z_\.]+$/;
const MatchKeyNumber = /^\d+$/;
const MatchInt = /^-?\d+$/;
const MatchDouble = /^-?\d+(\.\d+)?$/;
const MatchDouble2 = /^-?\.\d+$/;
Expand Down Expand Up @@ -850,7 +851,7 @@ export default class KeyValues3 {
let prefix = '';
const root = this.IsRoot();

if (MatchKeyNoQuote.test(this.Key)) {
if (MatchKeyNoQuote.test(this.Key) && !MatchKeyNumber.test(this.Key)) {
prefix = `${tab}${this.Key} =`;
} else {
prefix = `${tab}"${this.Key}" =`;
Expand Down

0 comments on commit 83c07bc

Please sign in to comment.