Sha256: 1d0a4e941c1504dcd9bc6cfbb77f7b44d93e56a29cba6f2ccebd78d501a51c16

Contents?: true

Size: 1.55 KB

Versions: 34

Compression:

Stored size: 1.55 KB

Contents

/*!
 * 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\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\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)
}

Version data entries

34 entries across 33 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/encodeurl/index.js
disco_app-0.18.0 test/dummy/node_modules/encodeurl/index.js
disco_app-0.18.2 test/dummy/node_modules/encodeurl/index.js
disco_app-0.16.1 test/dummy/node_modules/encodeurl/index.js
disco_app-0.15.2 test/dummy/node_modules/encodeurl/index.js
disco_app-0.18.4 test/dummy/node_modules/encodeurl/index.js
disco_app-0.18.1 test/dummy/node_modules/encodeurl/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/encodeurl/index.js
disco_app-0.14.0 test/dummy/node_modules/encodeurl/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/encodeurl/index.js
tang-0.2.1 spec/tang_app/node_modules/encodeurl/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/encodeurl/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/encodeurl/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/encodeurl/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/encodeurl/index.js
tang-0.2.0 spec/tang_app/node_modules/encodeurl/index.js
tang-0.1.0 spec/tang_app/node_modules/encodeurl/index.js
tang-0.0.9 spec/tang_app/node_modules/encodeurl/index.js
enju_library-0.3.8 spec/dummy/node_modules/encodeurl/index.js
ilog-0.4.1 node_modules/encodeurl/index.js