All files / mp getCurrentPageQuery.ts

100% Statements 5/5
83.33% Branches 5/6
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                          2x 2x 2x   4x   2x      
import { AnyObject } from '../types'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { last, mapValues } from '../utils'
 
/**
 * 获取当前页面的查询参数,已经对每个值执行了 decodeURIComponent。
 *
 * @param pageInstance 页面实例,默认当前页面
 * @returns 返回当前页面的查询参数
 */
export function getCurrentPageQuery<
  T extends Record<string, string | undefined>,
>(pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>): T {
  return ensureInMiniProgram(() => {
    pageInstance = pageInstance || last(getCurrentPages())!
    const query = mapValues(
      pageInstance.options || {},
      value => value && decodeURIComponent(value),
    )
    return query as any as T
  })
}