Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 71 additions & 70 deletions lib/node_modules/@stdlib/string/base/percent-encode/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,101 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

"use strict";

// MODULES //

var utf16ToUTF8Array = require( '@stdlib/string/utf16-to-utf8-array' );

var utf16ToUTF8Array = require("@stdlib/string/utf16-to-utf8-array");

// VARIABLES //

// Character codes:
var UNDERSCORE = 95|0;
var PERIOD = 46|0;
var HYPHEN = 45|0;
var TILDE = 126|0;
var ZERO = 48|0;
var NINE = 57|0;
var A = 65|0;
var Z = 90|0;
var a = 97|0;
var z = 122|0;

var UNDERSCORE = 95 | 0;
var PERIOD = 46 | 0;
var HYPHEN = 45 | 0;
var TILDE = 126 | 0;
var ZERO = 48 | 0;
var NINE = 57 | 0;
var A = 65 | 0;
var Z = 90 | 0;
var a = 97 | 0;
var z = 122 | 0;

// MAIN //

/**
* Percent-encodes a UTF-16 encoded string according to [RFC 3986][1].
*
* [1]: https://tools.ietf.org/html/rfc3986#section-2.1
*
* @param {string} str - string to percent-encode
* @returns {string} percent-encoded string
*
* @example
* var str1 = 'Ladies + Gentlemen';
*
* var str2 = percentEncode( str1 );
* // returns 'Ladies%20%2B%20Gentlemen'
*/
function percentEncode( str ) {
* Percent-encodes a UTF-16 encoded string according to [RFC 3986][1].
*
* [1]: https://tools.ietf.org/html/rfc3986#section-2.1
*
* @param {string} str - string to percent-encode
* @returns {string} percent-encoded string
*
* @example
* var str1 = 'Ladies + Gentlemen';
*
* var str2 = percentEncode( str1 );
* // returns 'Ladies%20%2B%20Gentlemen'
*/
function percentEncode(str) {
var byte;
var out;
var len;
var buf;
var i;

// Convert to an array of octets using UTF-8 encoding (see https://tools.ietf.org/html/rfc3986#section-2.5):
buf = utf16ToUTF8Array( str );
buf = utf16ToUTF8Array(str);

// Encode the string...
len = buf.length;
out = '';
for ( i = 0; i < len; i++ ) {
byte = buf[ i ];
if (
// ASCII Digits:
( byte >= ZERO && byte <= NINE ) ||

// ASCII uppercase letters:
( byte >= A && byte <= Z ) ||

// ASCII lowercase letters:
( byte >= a && byte <= z ) ||

// ASCII unreserved characters (see https://tools.ietf.org/html/rfc3986#section-2.3):
byte === HYPHEN ||
byte === PERIOD ||
byte === UNDERSCORE ||
byte === TILDE
) {
out += str.charAt( i );
} else {
// Convert an octet to hexadecimal and uppercase according to the RFC (see https://tools.ietf.org/html/rfc3986#section-2.1):
out += '%' + byte.toString( 16 ).toUpperCase();
out = "";
for (i = 0; i < len; i++) {
byte = buf[i];
// Encode the string...
len = buf.length;
out = "";
for (i = 0; i < len; i++) {
byte = buf[i];
if (
// ASCII Digits:
(byte >= ZERO && byte <= NINE) ||
// ASCII uppercase letters:
(byte >= A && byte <= Z) ||
// ASCII lowercase letters:
(byte >= a && byte <= z) ||
// ASCII unreserved characters (see https://tools.ietf.org/html/rfc3986#section-2.3):
byte === HYPHEN ||
byte === PERIOD ||
byte === UNDERSCORE ||
byte === TILDE
) {
out += String.fromCharCode(byte);
} else {
// Convert an octet to hexadecimal and uppercase according to the RFC (see https://tools.ietf.org/html/rfc3986#section-2.1):
var hex = byte.toString(16).toUpperCase();
out += "%" + (hex.length === 1 ? "0" : "") + hex;
}
}
}
return out;
}


// EXPORTS //

module.exports = percentEncode;
91 changes: 48 additions & 43 deletions lib/node_modules/@stdlib/string/base/percent-encode/test/test.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,74 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';
"use strict";

// MODULES //

var tape = require( 'tape' );
var percentEncode = require( './../lib' );

var tape = require("tape");
var percentEncode = require("./../lib");

// TESTS //

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof percentEncode, 'function', 'main export is a function' );
tape("main export is a function", function test(t) {
t.ok(true, __filename);
t.strictEqual(typeof percentEncode, "function", "main export is a function");
t.end();
});

tape( 'the function percent-encodes a string', function test( t ) {
tape("the function percent-encodes a string", function test(t) {
var expected;
var values;
var out;
var i;

// Some test values are from https://dev.twitter.com/oauth/overview/percent-encoding-parameters.
values = [
'Ladies + Gentlemen',
'An encoded string!',
'Dogs, Cats & Mice',
'☃',
'æ',
'𐐷',
'()',
'https://en.wikipedia.org/wiki/Mode_(statistics)'
"Ladies + Gentlemen",
"An encoded string!",
"Dogs, Cats & Mice",
"☃",
"æ",
"𐐷",
"()",
"https://en.wikipedia.org/wiki/Mode_(statistics)",
"\t",
"\r",
"\é1",
];

expected = [
'Ladies%20%2B%20Gentlemen',
'An%20encoded%20string%21',
'Dogs%2C%20Cats%20%26%20Mice',
'%E2%98%83',
'%C3%A6',
'%F0%90%90%B7',
'%28%29',
'https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMode_%28statistics%29'
"Ladies%20%2B%20Gentlemen",
"An%20encoded%20string%21",
"Dogs%2C%20Cats%20%26%20Mice",
"%E2%98%83",
"%C3%A6",
"%F0%90%90%B7",
"%28%29",
"https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMode_%28statistics%29",
"%09",
"%0D",
"%C3%A91",
];

for ( i = 0; i < values.length; i++ ) {
out = percentEncode( values[ i ] );
t.strictEqual( out, expected[ i ], 'returns expected value for '+values[i] );
for (i = 0; i < values.length; i++) {
out = percentEncode(values[i]);
t.strictEqual(out, expected[i], "returns expected value for " + values[i]);
}
t.end();
});
Loading