update
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
var inspect = require('../');
|
||||
var test = require('tape');
|
||||
var arrow = require('make-arrow-function')();
|
||||
var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();
|
||||
|
||||
test('function', function (t) {
|
||||
t.plan(1);
|
||||
var obj = [1, 2, function f(n) { return n; }, 4];
|
||||
t.equal(inspect(obj), '[ 1, 2, [Function: f], 4 ]');
|
||||
});
|
||||
|
||||
test('function name', function (t) {
|
||||
t.plan(1);
|
||||
var f = (function () {
|
||||
return function () {};
|
||||
}());
|
||||
f.toString = function toStr() { return 'function xxx () {}'; };
|
||||
var obj = [1, 2, f, 4];
|
||||
t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)] { toString: [Function: toStr] }, 4 ]');
|
||||
});
|
||||
|
||||
test('anon function', function (t) {
|
||||
var f = (function () {
|
||||
return function () {};
|
||||
}());
|
||||
var obj = [1, 2, f, 4];
|
||||
t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)], 4 ]');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('arrow function', { skip: !arrow }, function (t) {
|
||||
t.equal(inspect(arrow), '[Function (anonymous)]');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('truly nameless function', { skip: !arrow || !functionsHaveConfigurableNames }, function (t) {
|
||||
function f() {}
|
||||
Object.defineProperty(f, 'name', { value: false });
|
||||
t.equal(f.name, false);
|
||||
t.equal(
|
||||
inspect(f),
|
||||
'[Function: f]',
|
||||
'named function with falsy `.name` does not hide its original name'
|
||||
);
|
||||
|
||||
function g() {}
|
||||
Object.defineProperty(g, 'name', { value: true });
|
||||
t.equal(g.name, true);
|
||||
t.equal(
|
||||
inspect(g),
|
||||
'[Function: true]',
|
||||
'named function with truthy `.name` hides its original name'
|
||||
);
|
||||
|
||||
var anon = function () {}; // eslint-disable-line func-style
|
||||
Object.defineProperty(anon, 'name', { value: null });
|
||||
t.equal(anon.name, null);
|
||||
t.equal(
|
||||
inspect(anon),
|
||||
'[Function (anonymous)]',
|
||||
'anon function with falsy `.name` does not hide its anonymity'
|
||||
);
|
||||
|
||||
var anon2 = function () {}; // eslint-disable-line func-style
|
||||
Object.defineProperty(anon2, 'name', { value: 1 });
|
||||
t.equal(anon2.name, 1);
|
||||
t.equal(
|
||||
inspect(anon2),
|
||||
'[Function: 1]',
|
||||
'anon function with truthy `.name` hides its anonymity'
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
The ISC License
|
||||
|
||||
Copyright (c) 2015, 2019 Elan Shanker
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
||||
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
@@ -0,0 +1,60 @@
|
||||
/*!
|
||||
* encodeurl
|
||||
* Copyright(c) 2016 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
* @public
|
||||
*/
|
||||
|
||||
module.exports = encodeUrl
|
||||
|
||||
/**
|
||||
* RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
|
||||
* and including invalid escape sequences.
|
||||
* @private
|
||||
*/
|
||||
|
||||
var ENCODE_CHARS_REGEXP = /(?:[^\x21\x23-\x3B\x3D\x3F-\x5F\x61-\x7A\x7C\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g
|
||||
|
||||
/**
|
||||
* RegExp to match unmatched surrogate pair.
|
||||
* @private
|
||||
*/
|
||||
|
||||
var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g
|
||||
|
||||
/**
|
||||
* String to replace unmatched surrogate pair with.
|
||||
* @private
|
||||
*/
|
||||
|
||||
var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2'
|
||||
|
||||
/**
|
||||
* Encode a URL to a percent-encoded form, excluding already-encoded sequences.
|
||||
*
|
||||
* This function will take an already-encoded URL and encode all the non-URL
|
||||
* code points. This function will not encode the "%" character unless it is
|
||||
* not part of a valid sequence (`%20` will be left as-is, but `%foo` will
|
||||
* be encoded as `%25foo`).
|
||||
*
|
||||
* This encode is meant to be "safe" and does not throw errors. It will try as
|
||||
* hard as it can to properly encode the given URL, including replacing any raw,
|
||||
* unpaired surrogate pairs with the Unicode replacement character prior to
|
||||
* encoding.
|
||||
*
|
||||
* @param {string} url
|
||||
* @return {string}
|
||||
* @public
|
||||
*/
|
||||
|
||||
function encodeUrl (url) {
|
||||
return String(url)
|
||||
.replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
|
||||
.replace(ENCODE_CHARS_REGEXP, encodeURI)
|
||||
}
|
||||
Reference in New Issue
Block a user