Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby) Convert get-value-at to typescript #22182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/gatsby/src/db/loki/nodes-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
objectToDottedField,
liftResolvedFields,
} = require(`../common/query`)
const { getValueAt } = require(`../../utils/get-value-at`)
import { getValueAt } from "../../utils/get-value-at"
const { runSiftOnNodes } = require(`../../redux/run-sift`)

// Takes a raw graphql filter and converts it into a mongo-like args
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { default: sift } = require(`sift`)
const _ = require(`lodash`)
const { prepareRegex } = require(`../utils/prepare-regex`)
const { makeRe } = require(`micromatch`)
const { getValueAt } = require(`../utils/get-value-at`)
import { getValueAt } from "../utils/get-value-at"
const {
toDottedFields,
objectToDottedField,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer/is-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { slash } = require(`gatsby-core-utils`)
const mime = require(`mime`)
const isRelative = require(`is-relative`)
const isRelativeUrl = require(`is-relative-url`)
const { getValueAt } = require(`../../utils/get-value-at`)
import { getValueAt } from "../../utils/get-value-at"

const isFile = (nodeStore, fieldPath, relativePath) => {
const filePath = getFilePath(nodeStore, fieldPath, relativePath)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const systemPath = require(`path`)
const normalize = require(`normalize-path`)
const _ = require(`lodash`)
const { GraphQLList, getNullableType, getNamedType, Kind } = require(`graphql`)
const { getValueAt } = require(`../utils/get-value-at`)
import { getValueAt } from "../utils/get-value-at"

const findMany = typeName => (source, args, context, info) =>
context.nodeModel.runQuery(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getValueAt } = require(`../get-value-at`)
import { getValueAt } from "../get-value-at"

describe(`getValueAt util`, () => {
it(`handles object properties`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const getValueAt = (obj, selector) => {
export function getValueAt(obj: object, selector: string | string[]): any {
const selectors =
typeof selector === `string` ? selector.split(`.`) : selector
return get(obj, selectors)
}

const get = (obj, selectors) => {
function get(obj: object, selectors: string[]): any {
if (Array.isArray(obj)) return getArray(obj, selectors)
const [key, ...rest] = selectors
const value = obj[key]
Expand All @@ -14,11 +14,10 @@ const get = (obj, selectors) => {
return undefined
}

const getArray = (arr, selectors) =>
arr
function getArray(arr: object[], selectors: string[]): any[] {
return arr
.map(value =>
Array.isArray(value) ? getArray(value, selectors) : get(value, selectors)
)
.filter(v => v !== undefined)

module.exports = { getValueAt }
}