Add Ref.maybeParse.

This commit is contained in:
Benjamin Kalman
2016-03-04 13:37:56 -08:00
parent 4e849aeb02
commit 1b3fdbf380
2 changed files with 9 additions and 2 deletions

View File

@@ -82,6 +82,11 @@ export default class Ref {
return new Ref(m[1]);
}
static maybeParse(s: string): ?Ref {
const m = s.match(pattern);
return m ? new Ref(m[1]) : null;
}
static fromDigest(digest: Uint8Array = new Uint8Array(sha1Size)) {
return new Ref(sha1Prefix + uint8ArrayToHex(digest));
}

View File

@@ -12,6 +12,7 @@ suite('Ref', () => {
assert.throws(() => {
Ref.parse(s);
});
assert.equal(null, Ref.maybeParse(s));
}
assertParseError('foo');
@@ -27,8 +28,9 @@ suite('Ref', () => {
// sha2 not supported
assertParseError('sha2-0000000000000000000000000000000000000000');
const r = Ref.parse('sha1-0000000000000000000000000000000000000000');
assert.isNotNull(r);
const valid = 'sha1-0000000000000000000000000000000000000000';
assert.isNotNull(Ref.parse(valid));
assert.isNotNull(Ref.maybeParse(valid));
});
test('equals', () => {