shell-ahk
Version:
`Lodash`/`jQuery` for `AHK`.
35 lines (29 loc) • 891 B
text/coffeescript
# @ts-check
import at_isArray from './isArray'
import at_split from './split'
import at_length from './length'
###* @type import('./at').At ###
export default (ipt, paths...) ->
# Array access: support negative indices
if at_isArray ipt
at_index = paths[0]
if at_index < 0
return ipt[at_length(ipt) + at_index]
return ipt[at_index]
# Object access: support dot notation and multiple path segments
at_path = if at_length(paths) == 1
String paths[0]
else
at_segments = []
for at_p in paths
at_parts = at_split String(at_p), '.'
for at_part in at_parts
if at_part then at_segments.push at_part
at_segments.join '.'
# Traverse the object path
at_keys = at_split at_path, '.'
at_result = ipt
for at_key in at_keys
if at_result == undefined then return undefined
at_result = at_result[at_key]
return at_result