Skip to content

Commit

Permalink
Fix brace support on GUID's. (#16)
Browse files Browse the repository at this point in the history
* Support brackets on parsing GUID's.
  • Loading branch information
Corniel Nobel committed Sep 24, 2018
1 parent 33b17f8 commit 0760a44
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 31 deletions.
24 changes: 16 additions & 8 deletions src/Qowaiv.TypeScript/Guid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
/**
* Creates a GUID from a JSON string.
* @param {string} s A JSON string representing the GUID.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
public static fromJSON(s: string): Guid {
return Guid.parse(s);
Expand All @@ -77,31 +77,39 @@
* to avoid a create() call.
*/
public static isValid(s: string): boolean {
return /^[0-9ABCDEF]{32}$/i.test(s.replace(/-/g, ''));
return /^[0-9ABCDEF]{32}$/i.test(Guid.strip(s));
}

/**
* Creates a GUID.
* @param {string} s A string containing GUID to convert or a number.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
public static parse(s: string): Guid {

// an empty string should equal Guid.Empty.
if (s === '') { return new Guid(); }


s = Guid.strip(s).toUpperCase();

// if the value parameter is valid
if (Guid.isValid(s)) {
var guid = new Guid();
s = s.replace(/-/g, '').toUpperCase();
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{8})/, '$1-$2-$3-$4-$5');
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
return guid;
}

// return null if creation failed.
return null;
}

private static strip(s: string): string {
var replace = s.replace(/-/g, '');
if (replace.indexOf('{') == 0 && replace.lastIndexOf('}') == replace.length - 1) {
replace = replace.substr(1, replace.length - 2);
}
return replace;
}

/**
* Returns a new empty GUID.
*/
Expand All @@ -111,7 +119,7 @@

/**
* Creates a GUID.
* @returns A random GUID.
* @returns {Guid} A random GUID.
*/
public static newGuid(seed?: Guid): Guid {

Expand Down
11 changes: 6 additions & 5 deletions src/Qowaiv.TypeScript/JavaScript/Qowaiv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare module Qowaiv {
/**
* Creates a GUID from a JSON string.
* @param {string} s A JSON string representing the GUID.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
static fromJSON(s: string): Guid;
/**
Expand All @@ -50,23 +50,24 @@ declare module Qowaiv {
/**
* Creates a GUID.
* @param {string} s A string containing GUID to convert or a number.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
static parse(s: string): Guid;
private static strip;
/**
* Returns a new empty GUID.
*/
static empty(): Guid;
/**
* Creates a GUID.
* @returns A random GUID.
* @returns {Guid} A random GUID.
*/
static newGuid(seed?: Guid): Guid;
/**
* Creates random GUID blocks.
* @remarks called 4 times by Guid.newGuid().
*/
private static rndGuid(s);
private static rndGuid;
}
}
/**
Expand Down Expand Up @@ -122,7 +123,7 @@ declare module Qowaiv {
* @constructor
*/
constructor(d?: number, h?: number, m?: number, s?: number, f?: number);
private num(n);
private num;
/**
* Returns the days of the time span.
*/
Expand Down
19 changes: 13 additions & 6 deletions src/Qowaiv.TypeScript/JavaScript/Qowaiv.js

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

2 changes: 1 addition & 1 deletion src/Qowaiv.TypeScript/JavaScript/Qowaiv.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/Qowaiv.TypeScript/JavaScript/Qowaiv.min.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ declare module Qowaiv {
static fromJSON(s: string): Guid;
static isValid(s: string): boolean;
static parse(s: string): Guid;
private static strip;
static empty(): Guid;
static newGuid(seed?: Guid): Guid;
private static rndGuid(s);
private static rndGuid;
}
}
interface IEquatable {
Expand All @@ -30,7 +31,7 @@ declare module Qowaiv {
private static pattern;
private v;
constructor(d?: number, h?: number, m?: number, s?: number, f?: number);
private num(n);
private num;
getDays(): number;
getHours(): number;
getMinutes(): number;
Expand Down
13 changes: 10 additions & 3 deletions src/Qowaiv.TypeScript/JavaScript/Qowaiv.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ var Qowaiv;
return Guid.parse(s);
}
static isValid(s) {
return /^[0-9ABCDEF]{32}$/i.test(s.replace(/-/g, ''));
return /^[0-9ABCDEF]{32}$/i.test(Guid.strip(s));
}
static parse(s) {
if (s === '') {
return new Guid();
}
s = Guid.strip(s).toUpperCase();
if (Guid.isValid(s)) {
var guid = new Guid();
s = s.replace(/-/g, '').toUpperCase();
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{8})/, '$1-$2-$3-$4-$5');
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
return guid;
}
return null;
}
static strip(s) {
var replace = s.replace(/-/g, '');
if (replace.indexOf('{') == 0 && replace.lastIndexOf('}') == replace.length - 1) {
replace = replace.substr(1, replace.length - 2);
}
return replace;
}
static empty() {
return new Guid();
}
Expand Down
12 changes: 12 additions & 0 deletions test/Qowaiv.TypeScript.UnitTests/GuidTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ describe("GUID: ", () => {
var guid = Qowaiv.Guid.parse("DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189");
expect(guid.format("s")).toBe("dc7fba65df6f4cb98faa6c7b5654f189");
});

it("Parse('{DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189}') should be parseable.", () => {

var guid = Qowaiv.Guid.parse("{DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189}");
expect(guid.format("U")).toBe("DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189");
});

it("Parse('Nonsense') should not be parseable.", () => {

var guid = Qowaiv.Guid.parse("Nonsense");
expect(guid).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<PropertyGroup>
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptJSXEmit>None</TypeScriptJSXEmit>
Expand Down
27 changes: 21 additions & 6 deletions test/Qowaiv.TypeScript.UnitTests/UnitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var Qowaiv;
/**
* Creates a GUID from a JSON string.
* @param {string} s A JSON string representing the GUID.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
Guid.fromJSON = function (s) {
return Guid.parse(s);
Expand All @@ -73,28 +73,35 @@ var Qowaiv;
* to avoid a create() call.
*/
Guid.isValid = function (s) {
return /^[0-9ABCDEF]{32}$/i.test(s.replace(/-/g, ''));
return /^[0-9ABCDEF]{32}$/i.test(Guid.strip(s));
};
/**
* Creates a GUID.
* @param {string} s A string containing GUID to convert or a number.
* @returns A GUID if valid, otherwise null.
* @returns {Guid} A GUID if valid, otherwise null.
*/
Guid.parse = function (s) {
// an empty string should equal Guid.Empty.
if (s === '') {
return new Guid();
}
s = Guid.strip(s).toUpperCase();
// if the value parameter is valid
if (Guid.isValid(s)) {
var guid = new Guid();
s = s.replace(/-/g, '').toUpperCase();
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{8})/, '$1-$2-$3-$4-$5');
guid.v = s.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
return guid;
}
// return null if creation failed.
return null;
};
Guid.strip = function (s) {
var replace = s.replace(/-/g, '');
if (replace.indexOf('{') == 0 && replace.lastIndexOf('}') == replace.length - 1) {
replace = replace.substr(1, replace.length - 2);
}
return replace;
};
/**
* Returns a new empty GUID.
*/
Expand All @@ -103,7 +110,7 @@ var Qowaiv;
};
/**
* Creates a GUID.
* @returns A random GUID.
* @returns {Guid} A random GUID.
*/
Guid.newGuid = function (seed) {
var guid = new Guid();
Expand Down Expand Up @@ -179,6 +186,14 @@ describe("GUID: ", function () {
var guid = Qowaiv.Guid.parse("DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189");
expect(guid.format("s")).toBe("dc7fba65df6f4cb98faa6c7b5654f189");
});
it("Parse('{DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189}') should be parseable.", function () {
var guid = Qowaiv.Guid.parse("{DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189}");
expect(guid.format("U")).toBe("DC7FBA65-DF6F-4CB9-8FAA-6C7B5654F189");
});
it("Parse('Nonsense') should not be parseable.", function () {
var guid = Qowaiv.Guid.parse("Nonsense");
expect(guid).toBe(null);
});
});
var Qowaiv;
(function (Qowaiv) {
Expand Down

0 comments on commit 0760a44

Please sign in to comment.