-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iskey): adding new util to match property names and paths
- Loading branch information
1 parent
6ddf3d1
commit 97e60ed
Showing
4 changed files
with
92 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,35 @@ | ||
import { isKey } from './isKey'; | ||
|
||
describe('isKey', () => { | ||
describe('when only passing the value argument', () => { | ||
it('should get true', () => { | ||
expect(isKey(1)).toEqual(true); | ||
expect(isKey(Symbol())).toEqual(true); | ||
expect(isKey('attribute')).toEqual(true); | ||
expect(isKey('spaced attribute')).toEqual(true); | ||
expect(isKey(true)).toEqual(true); | ||
expect(isKey(null)).toEqual(true); | ||
expect(isKey(undefined)).toEqual(true); | ||
}); | ||
it('should get false', () => { | ||
expect(isKey([1, ''])).toEqual(false); | ||
expect(isKey('something[test]')).toEqual(false); | ||
expect(isKey('a.b')).toEqual(false); | ||
}); | ||
}); | ||
describe('when passing the value and an object', () => { | ||
const obj = { | ||
'[a]': 5, | ||
'b.c': true | ||
}; | ||
|
||
it('should get true', () => { | ||
expect(isKey('[a]', obj)).toEqual(true); | ||
expect(isKey('b.c', obj)).toEqual(true); | ||
}); | ||
it('should get false', () => { | ||
expect(isKey('[b]', obj)).toEqual(false); | ||
expect(isKey('c.d', obj)).toEqual(false); | ||
}); | ||
}); | ||
}); |
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,55 @@ | ||
import { isSymbol } from './isSymbol'; | ||
|
||
/** Used to match property names within property paths. */ | ||
const reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/; | ||
const reIsPlainProp = /^\w*$/; //maches any word caracter (alphanumeric and underscore) | ||
|
||
/** | ||
* Checks if `value` is a property name and not a property path. | ||
* | ||
* @private | ||
* @param {*} value The value to check. | ||
* @param {Object} [object] The object to query keys on. | ||
* @returns {boolean} Returns `true` if `value` is a property name, else `false`. | ||
* @example | ||
* ```javascript | ||
* isKey(1) | ||
* // => true | ||
* | ||
* isKey('example[test]') | ||
* // => false | ||
* | ||
* isKey('a.b') | ||
* // => false | ||
* | ||
* const obj = { | ||
* '[a]': 5, | ||
* 'b.c': true | ||
* }; | ||
* | ||
* isKey('[a]', obj) | ||
* // => true | ||
* | ||
* isKey('b.c', obj) | ||
* // => true | ||
* ``` | ||
*/ | ||
export function isKey(value: any, object?: object): boolean { | ||
if (Array.isArray(value)) { | ||
return false; | ||
} | ||
const type = typeof value; | ||
if ( | ||
type === 'number' || | ||
type === 'boolean' || | ||
value === null || | ||
isSymbol(value) | ||
) { | ||
return true; | ||
} | ||
return ( | ||
reIsPlainProp.test(value) || | ||
!reIsDeepProp.test(value) || | ||
(object !== null && value in Object(object)) | ||
); | ||
} |
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