Skip to content

Commit

Permalink
feat: add testObjectAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Mar 13, 2023
1 parent c4db609 commit 934c306
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/functions/address-object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {addressObject} from '../'
import {addressObject, testObjectAddress} from '../'

describe('Address Object', () => {
it('should address an object', () => {
Expand All @@ -17,5 +17,10 @@ describe('Address Object', () => {
expect(
addressObject(object, 'level1#level2#level3#sample', {seperator: '#'})
).toBe('pass')

expect(testObjectAddress(object, 'level1.level2')).toBe(true)
expect(testObjectAddress(object, 'level1.level1')).toBe(false)
expect(testObjectAddress(object, 'level1.level2.level4')).toBe(false)
expect(testObjectAddress(object, 'level2.level2.level4')).toBe(false)
})
})
23 changes: 23 additions & 0 deletions src/functions/address-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ export const addressObject = <ObjectType extends {[key: string]: any}>(
return value[key]
}, object)
}

export const testObjectAddress = <ObjectType extends {[key: string]: any}>(
object: ObjectType,
address: string,
options?: DeepPartial<AddressObjectOptions>
): boolean => {
const {seperator} = defaults(options, defaultOptions)

return address.split(seperator).reduce(
({currentObject, result}, key) => {
if (!result) {
return {currentObject, result}
}

if (currentObject[key]) {
return {currentObject: currentObject[key], result: true}
}

return {currentObject: {}, result: false}
},
{currentObject: object, result: true}
).result
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export {BitMask} from './classes/bit-mask'
export {Logger, LoggerOptions} from './classes/logger'

export {addressObject, AddressObjectOptions} from './functions/address-object'
export {
addressObject,
AddressObjectOptions,
testObjectAddress
} from './functions/address-object'
export {arrayMove} from './functions/array-move'
export {randomEntry} from './functions/array-selectors'
export {asyncForEach} from './functions/async-for-each'
Expand Down

0 comments on commit 934c306

Please sign in to comment.