-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { isPropertyKey } from "./isPropertyKey"; | ||
|
||
/** | ||
* Determine if given is a valid {@link import('@aedart/contracts/support').Key} | ||
* | ||
* @see {isPropertyKey} | ||
* | ||
* @param {any} key | ||
* | ||
* @returns {boolean} | ||
*/ | ||
export function isKey(key: any): boolean | ||
{ | ||
if (!Array.isArray(key)) { | ||
key = [ key ]; | ||
} | ||
|
||
if (key.length === 0) { | ||
return false; | ||
} | ||
|
||
for (const entry of key) { | ||
if (!isPropertyKey(entry)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
isKey | ||
} from '@aedart/support/misc'; | ||
|
||
describe('@aedart/support/misc', () => { | ||
describe('isKey', () => { | ||
|
||
it('can determine if value is a valid key', function () { | ||
const valid = [ | ||
0, | ||
1, | ||
-1, | ||
NaN, | ||
'foo', | ||
Symbol('my-symbol'), | ||
[ 'a', 'b.c' ], | ||
[ 'a', 'b.c', 12, Symbol('my-other-symbol') ], | ||
]; | ||
|
||
valid.forEach((value, index) => { | ||
expect(isKey(value)) | ||
.withContext(`Value at index ${index} is not a valid key`) | ||
.toBeTrue(); | ||
}); | ||
|
||
const invalid = [ | ||
[], | ||
() => false, | ||
{ name: 'John' }, | ||
true, | ||
false, | ||
null, | ||
undefined | ||
]; | ||
|
||
invalid.forEach((value, index) => { | ||
expect(isKey(value)) | ||
.withContext(`Invalid value at index ${index} SHOULD NOT be a valid key`) | ||
.toBeFalse(); | ||
}); | ||
}); | ||
}); | ||
}); |