11cb0ef41Sopenharmony_ci 21cb0ef41Sopenharmony_ci/** 31cb0ef41Sopenharmony_ci * Module exports. 41cb0ef41Sopenharmony_ci */ 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cimodule.exports = deprecate; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci/** 91cb0ef41Sopenharmony_ci * Mark that a method should not be used. 101cb0ef41Sopenharmony_ci * Returns a modified function which warns once by default. 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * If `localStorage.noDeprecation = true` is set, then it is a no-op. 131cb0ef41Sopenharmony_ci * 141cb0ef41Sopenharmony_ci * If `localStorage.throwDeprecation = true` is set, then deprecated functions 151cb0ef41Sopenharmony_ci * will throw an Error when invoked. 161cb0ef41Sopenharmony_ci * 171cb0ef41Sopenharmony_ci * If `localStorage.traceDeprecation = true` is set, then deprecated functions 181cb0ef41Sopenharmony_ci * will invoke `console.trace()` instead of `console.error()`. 191cb0ef41Sopenharmony_ci * 201cb0ef41Sopenharmony_ci * @param {Function} fn - the function to deprecate 211cb0ef41Sopenharmony_ci * @param {String} msg - the string to print to the console when `fn` is invoked 221cb0ef41Sopenharmony_ci * @returns {Function} a new "deprecated" version of `fn` 231cb0ef41Sopenharmony_ci * @api public 241cb0ef41Sopenharmony_ci */ 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cifunction deprecate (fn, msg) { 271cb0ef41Sopenharmony_ci if (config('noDeprecation')) { 281cb0ef41Sopenharmony_ci return fn; 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci var warned = false; 321cb0ef41Sopenharmony_ci function deprecated() { 331cb0ef41Sopenharmony_ci if (!warned) { 341cb0ef41Sopenharmony_ci if (config('throwDeprecation')) { 351cb0ef41Sopenharmony_ci throw new Error(msg); 361cb0ef41Sopenharmony_ci } else if (config('traceDeprecation')) { 371cb0ef41Sopenharmony_ci console.trace(msg); 381cb0ef41Sopenharmony_ci } else { 391cb0ef41Sopenharmony_ci console.warn(msg); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci warned = true; 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci return fn.apply(this, arguments); 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci return deprecated; 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci/** 501cb0ef41Sopenharmony_ci * Checks `localStorage` for boolean values for the given `name`. 511cb0ef41Sopenharmony_ci * 521cb0ef41Sopenharmony_ci * @param {String} name 531cb0ef41Sopenharmony_ci * @returns {Boolean} 541cb0ef41Sopenharmony_ci * @api private 551cb0ef41Sopenharmony_ci */ 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cifunction config (name) { 581cb0ef41Sopenharmony_ci // accessing global.localStorage can trigger a DOMException in sandboxed iframes 591cb0ef41Sopenharmony_ci try { 601cb0ef41Sopenharmony_ci if (!global.localStorage) return false; 611cb0ef41Sopenharmony_ci } catch (_) { 621cb0ef41Sopenharmony_ci return false; 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci var val = global.localStorage[name]; 651cb0ef41Sopenharmony_ci if (null == val) return false; 661cb0ef41Sopenharmony_ci return String(val).toLowerCase() === 'true'; 671cb0ef41Sopenharmony_ci} 68