Skip to content

Commit

Permalink
Add x format for struct-like GUIDs (#61)
Browse files Browse the repository at this point in the history
Resolves #60
  • Loading branch information
heaths authored Dec 9, 2022
1 parent 9d47573 commit 3efdb8d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
43 changes: 21 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"insertGuid.pasteAutomatically": {
"type": "string",
"default": "",
"markdownDescription": "Paste GUID without prompting using the specified format:\n* `{b}|{B}` inserts a braced string in lowercase `{b}` or uppercase `{B}` e.g., `{880c86bc-384c-4cce-9e9a-4f760ca755c4}`\n* `{d}|{D}` inserts a hyphenated string in lowercase `{d}` or uppercase `{D}` e.g., `880c86bc-384c-4cce-9e9a-4f760ca755c4`\n* `{n}|{N}` inserts an unformatted string in lowercase `{n}` or uppercase `{N}` e.g., `880c86bc384c4cce9e9a4f760ca755c4`\n\nOther characters will be interpreted literally e.g., `new GUID(\"{D}\")` inserts `new GUID(\"880C86BC-384C-4CCE-9E9A-4F760CA755C4\")`"
"markdownDescription": "Paste GUID without prompting using the specified format:\n* `{b}|{B}` inserts a braced string in lowercase `{b}` or uppercase `{B}` e.g., `{880c86bc-384c-4cce-9e9a-4f760ca755c4}`\n* `{d}|{D}` inserts a hyphenated string in lowercase `{d}` or uppercase `{D}` e.g., `880c86bc-384c-4cce-9e9a-4f760ca755c4`\n* `{n}|{N}` inserts an unformatted string in lowercase `{n}` or uppercase `{N}` e.g., `880c86bc384c4cce9e9a4f760ca755c4`\n* `{x}|{X}` inserts a struct-formatted string in lowercase `{x}` or uppercase `{X}` e.g., `{0x880c86bc,0x384c,0x4cce,{0x9e,0x9a,0x4f,0x76,0x0c,0xa7,0x55,0xc4}}`\n\nOther characters will be interpreted literally e.g., `new GUID(\"{D}\")` inserts `new GUID(\"880C86BC-384C-4CCE-9E9A-4F760CA755C4\")`"
}
}
},
Expand Down Expand Up @@ -85,8 +85,6 @@
"test": "node ./out/test/runTest.js"
},
"engines": {
"npm": "^6.14.0",
"node": "^14.18.0",
"vscode": "^1.30.0"
},
"dependencies": {
Expand All @@ -112,6 +110,7 @@
"mocha": "^9.0.3",
"mocha-junit-reporter": "^1.23.3",
"mocha-multi-reporters": "^1.1.7",
"source-map": "^0.7.4",
"trim-newlines": "^3.0.1",
"tslint": "^5.14.0",
"typescript": "^3.3.1",
Expand Down
12 changes: 10 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ export class GuidCommands {
return g.toString('no-hyphen').toUpperCase();
},
type: FormatType.UPPERCASE
}
},
{
format: (g) => {
return g.toString('x');
},
type: FormatType.SNIPPET
},
];

/**
Expand Down Expand Up @@ -194,7 +200,9 @@ export class GuidCommands {
'{d}': (g: Guid) => g.toString(),
'{D}': (g: Guid) => g.toString().toUpperCase(),
'{n}': (g: Guid) => g.toString('no-hyphen'),
'{N}': (g: Guid) => g.toString('no-hyphen').toUpperCase()
'{N}': (g: Guid) => g.toString('no-hyphen').toUpperCase(),
'{x}': (g: Guid) => g.toString('x'),
'{X}': (g: Guid) => g.toString('x').toUpperCase(),
}
const customFormatter = {
format: (g: Guid) => {
Expand Down
34 changes: 17 additions & 17 deletions src/test/guid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,87 +28,87 @@ suite('Guid', () => {
var g1 = new Guid();
var g2 = new Guid();

assert.notEqual(g1.toString(), g2.toString());
assert.notStrictEqual(g1.toString(), g2.toString());
});

test('can return empty Guid', () => {
var e = Guid.EMPTY;
assert.equal(e.toString(), '00000000-0000-0000-0000-000000000000');
assert.strictEqual(e.toString(), '00000000-0000-0000-0000-000000000000');
});

test('parses invalid Guid as empty', () => {
var buffer = Buffer.alloc(16);
buffer.fill(0);

var g = new Guid('z');
assert.deepEqual(g.toBuffer(), buffer);
assert.deepStrictEqual(g.toBuffer(), buffer);
});

test('parses Guid with curly braces', () => {
var buffer = Buffer.from([0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, ]);
var g = new Guid('{12341234-1234-1234-1234-123412341234}');

assert.deepEqual(g.toBuffer(), buffer);
assert.deepStrictEqual(g.toBuffer(), buffer);
})

test('parses Guid without curly braces', () => {
var buffer = Buffer.from([0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, ]);
var g = new Guid("12341234-1234-1234-1234-123412341234");

assert.deepEqual(g.toBuffer(), buffer);
assert.deepStrictEqual(g.toBuffer(), buffer);
});

test('returns 16-byte buffer', () => {
var g = new Guid();
assert.equal(g.toBuffer().length, 16);
assert.strictEqual(g.toBuffer().length, 16);
});

test('returns mutable buffer', () => {
var g = new Guid('12341234-1234-1234-1234-123412341234');
assert.equal(g.toString(), '12341234-1234-1234-1234-123412341234');
assert.strictEqual(g.toString(), '12341234-1234-1234-1234-123412341234');

var buffer = g.toBuffer();
buffer.fill(0xff);
assert.equal(g.toString(), 'ffffffff-ffff-ffff-ffff-ffffffffffff');
assert.strictEqual(g.toString(), 'ffffffff-ffff-ffff-ffff-ffffffffffff');
});

test('returns proper "struct" format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('struct'), '{0x01234567, 0x89ab, 0xcdef, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}');
assert.strictEqual(g.toString('struct'), '{0x01234567, 0x89ab, 0xcdef, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}');
});

test('returns proper "struct" format for "x"', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('x'), '{0x01234567, 0x89ab, 0xcdef, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}');
assert.strictEqual(g.toString('x'), '{0x01234567, 0x89ab, 0xcdef, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}');
});

test('returns proper "braced" format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('braced'), '{01234567-89ab-cdef-0123-456789abcdef}');
assert.strictEqual(g.toString('braced'), '{01234567-89ab-cdef-0123-456789abcdef}');
});

test('returns proper "braced" format for "b"', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('braced'), '{01234567-89ab-cdef-0123-456789abcdef}');
assert.strictEqual(g.toString('braced'), '{01234567-89ab-cdef-0123-456789abcdef}');
});

test('returns proper "no-hyphen" format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('no-hyphen'), '0123456789abcdef0123456789abcdef');
assert.strictEqual(g.toString('no-hyphen'), '0123456789abcdef0123456789abcdef');
});

test('returns proper string for missing format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString(), '01234567-89ab-cdef-0123-456789abcdef');
assert.strictEqual(g.toString(), '01234567-89ab-cdef-0123-456789abcdef');
});

test('returns proper string for invalid format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('invalid'), '01234567-89ab-cdef-0123-456789abcdef');
assert.strictEqual(g.toString('invalid'), '01234567-89ab-cdef-0123-456789abcdef');
});

test('returns proper string for non-string format', () => {
var g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString(undefined), '01234567-89ab-cdef-0123-456789abcdef');
assert.strictEqual(g.toString(undefined), '01234567-89ab-cdef-0123-456789abcdef');
});
});
});

0 comments on commit 3efdb8d

Please sign in to comment.