@@ -2,8 +2,8 @@ import test from './test.js'
22import { round } from './digit.js'
33/**
44 * @description 如果value小于min,取min;如果value大于max,取max
5- * @param {number } min
6- * @param {number } max
5+ * @param {number } min
6+ * @param {number } max
77 * @param {number } value
88 */
99function range ( min = 0 , max = 0 , value = 0 ) {
@@ -13,7 +13,7 @@ function range(min = 0, max = 0, value = 0) {
1313/**
1414 * @description 用于获取用户传递值的px值 如果用户传递了"xxpx"或者"xxrpx",取出其数值部分,如果是"xxxrpx"还需要用过uni.upx2px进行转换
1515 * @param {number|string } value 用户传递值的px值
16- * @param {boolean } unit
16+ * @param {boolean } unit
1717 * @returns {number|string }
1818 */
1919function getPx ( value , unit = false ) {
@@ -41,15 +41,15 @@ function sleep(value = 30) {
4141}
4242/**
4343 * @description 运行期判断平台
44- * @returns {string } 返回所在平台(小写)
44+ * @returns {string } 返回所在平台(小写)
4545 * @link 运行期判断平台 https://uniapp.dcloud.io/frame?id=判断平台
4646 */
4747function os ( ) {
4848 return uni . getSystemInfoSync ( ) . platform . toLowerCase ( )
4949}
5050/**
5151 * @description 获取系统信息同步接口
52- * @link 获取系统信息同步接口 https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync
52+ * @link 获取系统信息同步接口 https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync
5353 */
5454function sys ( ) {
5555 return uni . getSystemInfoSync ( )
@@ -179,22 +179,34 @@ function addUnit(value = 'auto', unit = uni?.$u?.config?.unit ?? 'px') {
179179/**
180180 * @description 深度克隆
181181 * @param {object } obj 需要深度克隆的对象
182+ * @param cache 缓存
182183 * @returns {* } 克隆后的对象或者原值(不是对象)
183184 */
184- function deepClone ( obj ) {
185- // 对常见的“非”值,直接返回原来值
186- if ( [ null , undefined , NaN , false ] . includes ( obj ) ) return obj
187- if ( typeof obj !== 'object' && typeof obj !== 'function' ) {
188- // 原始类型直接返回
189- return obj
190- }
191- const o = test . array ( obj ) ? [ ] : { }
192- for ( const i in obj ) {
193- if ( obj . hasOwnProperty ( i ) ) {
194- o [ i ] = typeof obj [ i ] === 'object' ? deepClone ( obj [ i ] ) : obj [ i ]
185+ function deepClone ( obj , cache = new WeakMap ( ) ) {
186+ if ( obj === null || typeof obj !== 'object' ) return obj ;
187+ if ( cache . has ( obj ) ) return cache . get ( obj ) ;
188+ let clone ;
189+ if ( obj instanceof Date ) {
190+ clone = new Date ( obj . getTime ( ) ) ;
191+ } else if ( obj instanceof RegExp ) {
192+ clone = new RegExp ( obj ) ;
193+ } else if ( obj instanceof Map ) {
194+ clone = new Map ( Array . from ( obj , ( [ key , value ] ) => [ key , deepClone ( value , cache ) ] ) ) ;
195+ } else if ( obj instanceof Set ) {
196+ clone = new Set ( Array . from ( obj , value => deepClone ( value , cache ) ) ) ;
197+ } else if ( Array . isArray ( obj ) ) {
198+ clone = obj . map ( value => deepClone ( value , cache ) ) ;
199+ } else if ( Object . prototype . toString . call ( obj ) === '[object Object]' ) {
200+ clone = Object . create ( Object . getPrototypeOf ( obj ) ) ;
201+ cache . set ( obj , clone ) ;
202+ for ( const [ key , value ] of Object . entries ( obj ) ) {
203+ clone [ key ] = deepClone ( value , cache ) ;
195204 }
205+ } else {
206+ clone = Object . assign ( { } , obj ) ;
196207 }
197- return o
208+ cache . set ( obj , clone ) ;
209+ return clone ;
198210}
199211
200212/**
@@ -205,24 +217,27 @@ function deepClone(obj) {
205217 */
206218function deepMerge ( target = { } , source = { } ) {
207219 target = deepClone ( target )
208- if ( typeof target !== 'object' || typeof source !== 'object' ) return false
220+ if ( typeof target !== 'object' || target === null || typeof source !== 'object' || source === null ) return target ;
221+ const merged = Array . isArray ( target ) ? target . slice ( ) : Object . assign ( { } , target ) ;
209222 for ( const prop in source ) {
210- if ( ! source . hasOwnProperty ( prop ) ) continue
211- if ( prop in target ) {
212- if ( typeof target [ prop ] !== 'object' ) {
213- target [ prop ] = source [ prop ]
214- } else if ( typeof source [ prop ] !== 'object' ) {
215- target [ prop ] = source [ prop ]
216- } else if ( target [ prop ] . concat && source [ prop ] . concat ) {
217- target [ prop ] = target [ prop ] . concat ( source [ prop ] )
218- } else {
219- target [ prop ] = deepMerge ( target [ prop ] , source [ prop ] )
220- }
223+ if ( ! source . hasOwnProperty ( prop ) ) continue ;
224+ const sourceValue = source [ prop ] ;
225+ const targetValue = merged [ prop ] ;
226+ if ( sourceValue instanceof Date ) {
227+ merged [ prop ] = new Date ( sourceValue ) ;
228+ } else if ( sourceValue instanceof RegExp ) {
229+ merged [ prop ] = new RegExp ( sourceValue ) ;
230+ } else if ( sourceValue instanceof Map ) {
231+ merged [ prop ] = new Map ( sourceValue ) ;
232+ } else if ( sourceValue instanceof Set ) {
233+ merged [ prop ] = new Set ( sourceValue ) ;
234+ } else if ( typeof sourceValue === 'object' && sourceValue !== null ) {
235+ merged [ prop ] = deepMerge ( targetValue , sourceValue ) ;
221236 } else {
222- target [ prop ] = source [ prop ]
237+ merged [ prop ] = sourceValue ;
223238 }
224239 }
225- return target
240+ return merged ;
226241}
227242
228243/**
@@ -327,7 +342,7 @@ if (!String.prototype.padStart) {
327342/**
328343 * @description 时间戳转为多久之前
329344 * @param {String|Number } timestamp 时间戳
330- * @param {String|Boolean } format
345+ * @param {String|Boolean } format
331346 * 格式化规则如果为时间格式字符串,超出一定时间范围,返回固定的时间格式;
332347 * 如果为布尔值false,无论什么时间,都返回多久以前的格式
333348 * @returns {string } 转化后的内容
@@ -517,7 +532,7 @@ function priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparato
517532 while ( re . test ( s [ 0 ] ) ) {
518533 s [ 0 ] = s [ 0 ] . replace ( re , `$1${ sep } $2` )
519534 }
520-
535+
521536 if ( ( s [ 1 ] || '' ) . length < prec ) {
522537 s [ 1 ] = s [ 1 ] || ''
523538 s [ 1 ] += new Array ( prec - s [ 1 ] . length + 1 ) . join ( '0' )
@@ -531,7 +546,7 @@ function priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparato
531546 * 比如以30位阈值,那么300大于30,可以理解为用户想要的是300ms,而不是想花300s去执行一个动画
532547 * @param {String|number } value 比如: "1s"|"100ms"|1|100
533548 * @param {boolean } unit 提示: 如果是false 默认返回number
534- * @return {string|number }
549+ * @return {string|number }
535550 */
536551function getDuration ( value , unit = true ) {
537552 const valueNum = parseInt ( value )
@@ -650,6 +665,16 @@ function pages() {
650665 return pages
651666}
652667
668+ /**
669+ * 获取页面历史栈指定层实例
670+ * @param back {number} [0] - 0或者负数,表示获取历史栈的哪一层,0表示获取当前页面实例,-1 表示获取上一个页面实例。默认0。
671+ */
672+ function getHistoryPage ( back = 0 ) {
673+ const pages = getCurrentPages ( )
674+ const len = pages . length
675+ return pages [ len - 1 + back ]
676+ }
677+
653678/**
654679 * @description 修改uView内置属性值
655680 * @param {object } props 修改内置props属性
@@ -701,5 +726,6 @@ export default {
701726 setProperty,
702727 page,
703728 pages,
729+ getHistoryPage,
704730 setConfig
705731}
0 commit comments