Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webidl-conversions

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webidl-conversions - npm Package Compare versions

Comparing version 1.2.1 to 2.0.0

LICENSE.md

184

lib/index.js

@@ -0,21 +1,183 @@

"use strict";
var conversions = {};
export default conversions;
module.exports = conversions;
var global_String = String;
var Number_isFinite = Number.isFinite;
function sign(x) {
return x < 0 ? -1 : 1;
}
conversions["boolean"] = val => !!val;
function evenRound(x) {
// Round x to the nearest integer, choosing the even integer if it lies halfway between two.
if ((x % 1) === 0.5 && (x & 1) === 0) { // [even number].5; round down (i.e. floor)
return Math.floor(x);
} else {
return Math.round(x);
}
}
conversions["DOMString"] = val => global_String(val);
function createNumberConversion(bitLength, opts) {
if (!opts.isUnsigned) {
--bitLength;
}
const lowerBound = opts.isUnsigned ? 0 : -Math.pow(2, bitLength);
const upperBound = Math.pow(2, bitLength) - 1;
conversions["double"] = val => {
var asNumber = +val;
const moduloVal = opts.moduloBitLength ? Math.pow(2, opts.moduloBitLength) : Math.pow(2, bitLength);
const moduloBound = opts.moduloBitLength ? Math.pow(2, opts.moduloBitLength - 1) : Math.pow(2, bitLength - 1);
if (!Number_isFinite(asNumber)) {
throw new TypeError("Argument is not a finite floating-point value.");
return function(V, opts) {
if (!opts) opts = {};
let x = +V;
if (opts.enforceRange) {
if (!Number.isFinite(x)) {
throw new TypeError("Argument is not a finite number");
}
x = sign(x) * Math.floor(Math.abs(x));
if (x < lowerBound || x > upperBound) {
throw new TypeError("Argument is not in byte range");
}
return x;
}
if (!isNaN(x) && opts.clamp) {
x = evenRound(x);
if (x < lowerBound) x = lowerBound;
if (x > upperBound) x = upperBound;
return x;
}
if (!Number.isFinite(x) || x === 0) {
return 0;
}
x = sign(x) * Math.floor(Math.abs(x));
x = x % moduloVal;
if (!opts.isUnsigned && x >= moduloBound) {
return x - moduloVal;
}
return x;
}
}
return asNumber;
conversions["void"] = function () {
return undefined;
};
conversions["void"] = () => undefined;
conversions["boolean"] = function (val) {
return !!val;
};
conversions["byte"] = createNumberConversion(8, { unsigned: false });
conversions["octet"] = createNumberConversion(8, { unsigned: true });
conversions["short"] = createNumberConversion(16, { unsigned: false });
conversions["unsigned short"] = createNumberConversion(16, { unsigned: true });
conversions["long"] = createNumberConversion(32, { unsigned: false });
conversions["unsigned long"] = createNumberConversion(32, { unsigned: true });
conversions["long long"] = createNumberConversion(32, { unsigned: false, moduloBitLength: 64 });
conversions["unsigned long long"] = createNumberConversion(32, { unsigned: true, moduloBitLength: 64 });
conversions["double"] = function (V) {
const x = +V;
if (!Number.isFinite(x)) {
throw new TypeError("Argument is not a finite floating-point value");
}
return x;
};
conversions["unrestricted double"] = function (V) {
const x = +V;
if (isNaN(x)) {
throw new TypeError("Argument is NaN");
}
return x;
};
// not quite valid, but good enough for JS
conversions["float"] = conversions["double"];
conversions["unrestricted float"] = conversions["unrestricted double"];
conversions["DOMString"] = function (V, opts) {
if (!opts) opts = {};
if (opts.treatNullAsEmptyString && V === null) {
return "";
}
return String(V);
};
conversions["ByteString"] = function (V, opts) {
const x = String(V);
let c = undefined;
for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
if (c > 255) {
throw new TypeError("Argument is not a valid bytestring");
}
}
return x;
};
conversions["USVString"] = function (V) {
const S = String(V);
const n = S.length;
const U = [];
for (let i = 0; i < n; ++i) {
const c = S.charCodeAt(i);
if (c < 0xD800 || c > 0xDFFF) {
U.push(String.fromCodePoint(c));
} else if (0xDC00 <= c && c <= 0xDFFF) {
U.push(String.fromCodePoint(0xFFFD));
} else {
if (i === n - 1) {
U.push(String.fromCodePoint(0xFFFD));
} else {
const d = S.charCodeAt(i + 1);
if (0xDC00 <= d && d <= 0xDFFF) {
const a = c & 0x3FF;
const b = d & 0x3FF;
U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b));
++i;
} else {
U.push(String.fromCodePoint(0xFFFD));
}
}
}
}
return U;
};
conversions["Date"] = function (V, opts) {
if (!(V instanceof Date)) {
throw new TypeError("Argument is not a Date object");
}
if (isNaN(V)) {
return undefined;
}
return V;
};
conversions["RegExp"] = function (V, opts) {
if (!(V instanceof RegExp)) {
V = new RegExp(V);
}
return V;
};

18

package.json
{
"name": "webidl-conversions",
"version": "1.2.1",
"version": "2.0.0",
"description": "Implements the WebIDL algorithms for converting to and from JavaScript values",
"main": "lib/index.js",
"scripts": {
"test": "mocha --compilers js:mocha-traceur test/*.js"
"test": "mocha test/*.js"
},
"repository": "domenic/webidl-conversions",
"repository": "jsdom/webidl-conversions",
"keywords": [

@@ -21,12 +21,4 @@ "webidl",

"devDependencies": {
"mocha": "^1.21.4",
"mocha-traceur": "^2.0.0",
"traceur": "^0.0.79"
},
"browserify": {
"transform": [
"es6ify"
]
},
"traceur-runner": true
"mocha": "^1.21.4"
}
}

@@ -8,3 +8,3 @@ # WebIDL Type Conversions on JavaScript Values

```js
import conversions from "webidl-conversions";
const conversions = require("webidl-conversions");

@@ -30,3 +30,3 @@ function doStuff(x, y) {

So far only `boolean`, `DOMString`, `double`, and `void` types are implemented. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!
All of the numeric types are implemented (float being implemented as double) and some others are as well - check the source for all of them. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!

@@ -33,0 +33,0 @@ I'm not sure yet what the strategy will be for modifiers, e.g. [`[Clamp]`](http://heycam.github.io/webidl/#Clamp). Maybe something like `conversions["unsigned long"](x, { clamp: true })`? We'll see.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc