How to use stringToBytes method in wpt

Best JavaScript code snippet using wpt

logicAndStorageTests.js

Source:logicAndStorageTests.js Github

copy

Full Screen

...6const assert = require("chai").assert;7const moment = require("moment");8const ethUtil = require('ethereumjs-util');9/// @notice helper method which converts string to bytes10function stringToBytes(text) {11 return web3.utils.utf8ToHex(text);12}13/// @notice helper method which convers bytes to string14function bytesToString(bytes) {15 return web3.utils.hexToUtf8(bytes);16}17/// @notice helper function which computes contract address before transaction execution18async function precomputeContractAddress(deployer) {19 const currentNonce = await web3.eth.getTransactionCount(deployer);20 const futureAddress = ethUtil.bufferToHex(ethUtil.generateAddress(deployer, currentNonce));21 return futureAddress;22}23/// @notice These tests cover main functionalities for proxy and logic ProofOfExistence contracts24/// Starting with adding through getting information about files to25/// validations and circuit breaker which should stop contract in a way that26/// none else can interact (change state) with the contract27contract("LogicPoe", function(accounts) {28 var proxyPoeContract, logicPoeContract, logic;29 var zeroAddress = "0x0000000000000000000000000000000000000000";30 /// @notice prepares new set of contracts before each test31 beforeEach('Setup', async () => {32 proxyPoeContract = await ProxyPoe.new();33 logicPoeContract = await LogicPoe.new();34 await proxyPoeContract.upgradeTo(logicPoeContract.address, {from: accounts[0]});35 logic = await LogicPoe.at(proxyPoeContract.address);36 });37 describe("Logic & Storage via proxy", function() {38 describe("Adding files", function() {39 it('Should store file info in contract', async () => {40 //given41 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";42 const file = "0xABCDEFGH";43 const tags = "tag1;tag2;tag3";44 const dateAdded = moment().unix();45 const userAddress = accounts[0];46 //when47 const ipfsbytes = stringToBytes(ipfsAddr);48 const filebytes32 = stringToBytes(file);49 const tagsbytes32 = stringToBytes(tags);50 const tx = await logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress});51 //then52 truffleAssert.eventEmitted(tx, 'FileAdded', (ev) => {53 return bytesToString(ev.ipfsHash) === ipfsAddr &&54 bytesToString(ev.fileHash) === file &&55 bytesToString(ev.tags) === tags &&56 ev.dateAdded === dateAdded.toString() &&57 ev.owner === userAddress;58 }, "Incorrect event parameters read");59 });60 it('Should store my file info in contract', async () => {61 //given62 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";63 const file = "0xABCDEFGH";64 const tags = "tag1;tag2;tag3";65 const dateAdded = moment().unix();66 const userAddress = accounts[0];67 //when68 const ipfsbytes = stringToBytes(ipfsAddr);69 const filebytes32 = stringToBytes(file);70 const tagsbytes32 = stringToBytes(tags);71 const tx = await logic.addMyFile(dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress});72 //then73 truffleAssert.eventEmitted(tx, 'FileAdded', (ev) => {74 return bytesToString(ev.ipfsHash) === ipfsAddr &&75 bytesToString(ev.fileHash) === file &&76 bytesToString(ev.tags) === tags &&77 ev.dateAdded === dateAdded.toString() &&78 ev.owner === userAddress;79 }, "Incorrect event parameters read");80 });81 it('Should store someones\' file info in contract submitted by other', async () => {82 //given83 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";84 const file = "0xABCDEFGH";85 const tags = "tag1;tag2;tag3";86 const dateAdded = moment().unix();87 const userAddress = accounts[1];88 //when89 const ipfsbytes = stringToBytes(ipfsAddr);90 const filebytes32 = stringToBytes(file);91 const tagsbytes32 = stringToBytes(tags);92 const tx = await logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: accounts[0]});93 //then94 truffleAssert.eventEmitted(tx, 'FileAdded', (ev) => {95 return bytesToString(ev.ipfsHash) === ipfsAddr &&96 bytesToString(ev.fileHash) === file &&97 bytesToString(ev.tags) === tags &&98 ev.dateAdded === dateAdded.toString() &&99 ev.owner === userAddress;100 }, "Incorrect event parameters read");101 });102 it('Should not add file to contract when userAddress not passed', async () => {103 //given104 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";105 const file = "0xABCDEFGH";106 const tags = "tag1;tag2;tag3";107 const dateAdded = moment().unix();108 //when109 const ipfsbytes = stringToBytes(ipfsAddr);110 const filebytes32 = stringToBytes(file);111 const tagsbytes32 = stringToBytes(tags);112 await truffleAssert.fails(113 logic.addFile(zeroAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: accounts[0]}),114 //then115 truffleAssert.ErrorType.REVERT,116 "User Address cannot be empty"117 );118 });119 it('Should not add file to contract when ipfsHash is empty', async () => {120 //given121 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";122 const file = "0xABCDEFGH";123 const tags = "tag1;tag2;tag3";124 const dateAdded = moment().unix();125 const userAddress = accounts[0];126 //when127 const ipfsbytes = stringToBytes(ipfsAddr + "XXX");128 const filebytes32 = stringToBytes(file);129 const tagsbytes32 = stringToBytes(tags);130 await truffleAssert.fails(131 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),132 //then133 truffleAssert.ErrorType.REVERT,134 "IPFS address is not valid or empty"135 );136 });137 it('Should not add file to contract when ipfsHash is too long', async () => {138 //given139 const file = "0xABCDEFGH";140 const tags = "tag1;tag2;tag3";141 const dateAdded = moment().unix();142 const userAddress = accounts[0];143 //when144 const ipfsbytes = stringToBytes("");145 const filebytes32 = stringToBytes(file);146 const tagsbytes32 = stringToBytes(tags);147 await truffleAssert.fails(148 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),149 //then150 truffleAssert.ErrorType.REVERT,151 "IPFS address is not valid or empty"152 );153 });154 it('Should not add file to contract when fileHash not passed', async () => {155 //given156 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";157 const tags = "tag1;tag2;tag3";158 const dateAdded = moment().unix();159 const userAddress = accounts[0];160 //when161 const ipfsbytes = stringToBytes(ipfsAddr);162 const filebytes32 = stringToBytes("");163 const tagsbytes32 = stringToBytes(tags);164 await truffleAssert.fails(165 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),166 //then167 truffleAssert.ErrorType.REVERT,168 "File hash cannot be empty"169 );170 });171 it('Should not add file to contract when dateAdded is zero', async () => {172 //given173 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";174 const file = "0xABCDEFGH";175 const tags = "tag1;tag2;tag3";176 const dateAdded = 0;177 const userAddress = accounts[0];178 //when179 const ipfsbytes = stringToBytes(ipfsAddr);180 const filebytes32 = stringToBytes(file);181 const tagsbytes32 = stringToBytes(tags);182 await truffleAssert.fails(183 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),184 //then185 truffleAssert.ErrorType.REVERT,186 "Date Added has to be greater than 0"187 );188 });189 it('Should not add file which already exists in contract', async () => {190 //given191 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";192 const file = "0xABCDEFGH";193 const tags = "tag1;tag2;tag3";194 const dateAdded = moment().unix();195 const userAddress = accounts[0];196 //when197 const ipfsbytes = stringToBytes(ipfsAddr);198 const filebytes32 = stringToBytes(file);199 const tagsbytes32 = stringToBytes(tags);200 await logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),201 await truffleAssert.fails(202 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: accounts[0]}),203 //then204 truffleAssert.ErrorType.REVERT,205 "File hash exists in contract"206 );207 });208 });209 describe("Getting files", function() {210 it('Should get details of the file', async() => {211 //given212 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";213 const file = "0xABCDEFGH";214 const tags = "tag1;tag2;tag3";215 const dateAdded = moment().unix();216 const userAddress = accounts[0];217 //when218 const ipfsbytes = stringToBytes(ipfsAddr);219 const filebytes32 = stringToBytes(file);220 const tagsbytes32 = stringToBytes(tags);221 await logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress});222 //when223 const response = await logic.fileDetails(filebytes32);224 //then225 assert.equal(userAddress, response.holder)226 assert.equal(ipfsAddr, bytesToString(response.ipfsHash));227 assert.equal(tags, bytesToString(response.tags));228 assert.equal(dateAdded, response.dateAdded);229 });230 it('Should return zero address value when file does not exist', async () => {231 //given232 const filebytes32 = stringToBytes("0xABCDEFGH");233 //when234 const response = await logic.fileDetails(filebytes32);235 //then236 assert.equal(zeroAddress, response.holder);237 assert.equal(null, response.ipfsHash);238 assert.equal('0x0'.padEnd(66,'0'), response.tags);239 assert.equal(0, response.dateAdded);240 assert.equal('0x0'.padEnd(66,'0'), response.fileHash);241 });242 it('Should return count of my files', async () => {243 //given244 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";245 const tags = "tag1;tag2;tag3";246 const dateAdded = moment().unix();247 const userAddress = accounts[0];248 //when249 const ipfsbytes = stringToBytes(ipfsAddr);250 const tagsbytes32 = stringToBytes(tags);251 await logic.addMyFile(dateAdded, stringToBytes("0x123456"), tagsbytes32, ipfsbytes, {from: userAddress});252 await logic.addMyFile(dateAdded, stringToBytes("0xabcdef"), tagsbytes32, ipfsbytes, {from: userAddress});253 await logic.addMyFile(dateAdded, stringToBytes("0xtest"), tagsbytes32, ipfsbytes, {from: userAddress});254 const response = await logic.getCountOfMyfiles.call({from: userAddress});255 //then256 assert.equal("3", response)257 });258 it('Should return count of user files added by various entities', async () => {259 //given260 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";261 const tags = "tag1;tag2;tag3";262 const dateAdded = moment().unix();263 const userAddress = accounts[0];264 //when265 const ipfsbytes = stringToBytes(ipfsAddr);266 const tagsbytes32 = stringToBytes(tags);267 await logic.addFile(userAddress, dateAdded, stringToBytes("0x123456"), tagsbytes32, ipfsbytes, {from: accounts[1]});268 await logic.addFile(userAddress, dateAdded, stringToBytes("0xabcdef"), tagsbytes32, ipfsbytes, {from: accounts[2]});269 await logic.addFile(userAddress, dateAdded, stringToBytes("0xtest"), tagsbytes32, ipfsbytes, {from: accounts[0]});270 const response = await logic.getCountOfFiles.call(userAddress, {from: userAddress});271 //then272 assert.equal("3", response);273 });274 it('Should return my file details based on index', async () => {275 //given276 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";277 const tags = "tag1;tag2;tag3";278 const dateAdded = moment().unix();279 const userAddress = accounts[0];280 //when281 const ipfsbytes = stringToBytes(ipfsAddr);282 const tagsbytes32 = stringToBytes(tags);283 await logic.addMyFile(dateAdded, stringToBytes("0x123456"), tagsbytes32, ipfsbytes, {from: userAddress});284 await logic.addMyFile(dateAdded, stringToBytes("0xabcdef"), tagsbytes32, ipfsbytes, {from: userAddress});285 await logic.addMyFile(dateAdded, stringToBytes("0xtest"), tagsbytes32, ipfsbytes, {from: userAddress});286 const response = await logic.getMyFileById.call(2, {from: userAddress});287 //then288 assert.equal(dateAdded, response[0]);289 assert.equal("0xtest", bytesToString(response[1]));290 assert.equal(tags, bytesToString(response[2]));291 assert.equal(ipfsAddr, bytesToString(response[3]));292 });293 it('Should return user file details based on index', async () => {294 //given295 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";296 const tags = "tag1;tag2;tag3";297 const dateAdded = moment().unix();298 const userAddress = accounts[0];299 //when300 const ipfsbytes = stringToBytes(ipfsAddr);301 const tagsbytes32 = stringToBytes(tags);302 await logic.addFile(userAddress, dateAdded, stringToBytes("0x123456"), tagsbytes32, ipfsbytes, {from: accounts[1]});303 await logic.addFile(userAddress, dateAdded, stringToBytes("0xabcdef"), tagsbytes32, ipfsbytes, {from: accounts[1]});304 await logic.addFile(userAddress, dateAdded, stringToBytes("0xtest"), tagsbytes32, ipfsbytes, {from: accounts[1]});305 const response = await logic.getMyFileById.call(2, {from: userAddress});306 //then307 assert.equal(dateAdded, response[0]);308 assert.equal("0xtest", bytesToString(response[1]));309 assert.equal(tags, bytesToString(response[2]));310 assert.equal(ipfsAddr, bytesToString(response[3]));311 });312 });313 describe("Circuit Breaker", function (){314 it('Should stop contract from adding new files', async() => {315 //given316 const userAddress = accounts[0];317 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";318 const file = "0xABCDEFGH";319 const tags = "tag1;tag2;tag3";320 const dateAdded = moment().unix();321 const ipfsbytes = stringToBytes(ipfsAddr);322 const filebytes32 = stringToBytes(file);323 const tagsbytes32 = stringToBytes(tags);324 //when325 const tx = await logic.toggleContractState({from: userAddress});326 //then327 truffleAssert.eventEmitted(tx, 'StateChanged', (ev) => {328 return ev.who === userAddress &&329 ev.state === true;330 }, "Incorrect event parameters read");331 await truffleAssert.fails(332 logic.addFile(userAddress, dateAdded, filebytes32, tagsbytes32, ipfsbytes, {from: userAddress}),333 //then334 truffleAssert.ErrorType.REVERT,335 "Contract is disabled"336 );337 });338 it('Should not allow different user to disable contract', async() => {339 //given340 const userAddress = accounts[1];341 const ipfsAddr = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";342 const file = "0xABCDEFGH";343 const tags = "tag1;tag2;tag3";344 const dateAdded = moment().unix();345 const ipfsbytes = stringToBytes(ipfsAddr);346 const filebytes32 = stringToBytes(file);347 const tagsbytes32 = stringToBytes(tags);348 //when349 //then350 await truffleAssert.fails(351 logic.toggleContractState({from: userAddress}),352 //then353 truffleAssert.ErrorType.REVERT,354 "Access Denied"355 );356 });357 });358 });...

Full Screen

Full Screen

stringToBytes-Parse.spec.js

Source:stringToBytes-Parse.spec.js Github

copy

Full Screen

1const assert = require('assert');2const stringToBytes = require('../src/modules/stringToBytes');3describe('Test byte parse function', () => {4 it('Should return null if input is invalid', () => {5 assert.strictEqual(stringToBytes.parse(undefined), null);6 assert.strictEqual(stringToBytes.parse(null), null);7 assert.strictEqual(stringToBytes.parse(true), null);8 assert.strictEqual(stringToBytes.parse(false), null);9 assert.strictEqual(stringToBytes.parse(NaN), null);10 assert.strictEqual(stringToBytes.parse(() => {}), null);11 assert.strictEqual(stringToBytes.parse({}), null);12 });13 it('Should parse raw number', () => {14 assert.strictEqual(stringToBytes.parse(0), 0);15 assert.strictEqual(stringToBytes.parse(-1), -1);16 assert.strictEqual(stringToBytes.parse(1), 1);17 assert.strictEqual(stringToBytes.parse(10.5), 10.5);18 });19 it('Should parse KB', () => {20 assert.strictEqual(stringToBytes.parse('1kb'), 1 * Math.pow(1024, 1));21 assert.strictEqual(stringToBytes.parse('1KB'), 1 * Math.pow(1024, 1));22 assert.strictEqual(stringToBytes.parse('1Kb'), 1 * Math.pow(1024, 1));23 assert.strictEqual(stringToBytes.parse('1kB'), 1 * Math.pow(1024, 1));24 assert.strictEqual(stringToBytes.parse('0.5kb'), 0.5 * Math.pow(1024, 1));25 assert.strictEqual(stringToBytes.parse('0.5KB'), 0.5 * Math.pow(1024, 1));26 assert.strictEqual(stringToBytes.parse('0.5Kb'), 0.5 * Math.pow(1024, 1));27 assert.strictEqual(stringToBytes.parse('0.5kB'), 0.5 * Math.pow(1024, 1));28 assert.strictEqual(stringToBytes.parse('1.5kb'), 1.5 * Math.pow(1024, 1));29 assert.strictEqual(stringToBytes.parse('1.5KB'), 1.5 * Math.pow(1024, 1));30 assert.strictEqual(stringToBytes.parse('1.5Kb'), 1.5 * Math.pow(1024, 1));31 assert.strictEqual(stringToBytes.parse('1.5kB'), 1.5 * Math.pow(1024, 1));32 });33 it('Should parse MB', () => {34 assert.strictEqual(stringToBytes.parse('1mb'), 1 * Math.pow(1024, 2));35 assert.strictEqual(stringToBytes.parse('1MB'), 1 * Math.pow(1024, 2));36 assert.strictEqual(stringToBytes.parse('1Mb'), 1 * Math.pow(1024, 2));37 assert.strictEqual(stringToBytes.parse('1mB'), 1 * Math.pow(1024, 2));38 });39 it('Should parse GB', () => {40 assert.strictEqual(stringToBytes.parse('1gb'), 1 * Math.pow(1024, 3));41 assert.strictEqual(stringToBytes.parse('1GB'), 1 * Math.pow(1024, 3));42 assert.strictEqual(stringToBytes.parse('1Gb'), 1 * Math.pow(1024, 3));43 assert.strictEqual(stringToBytes.parse('1gB'), 1 * Math.pow(1024, 3));44 });45 it('Should parse TB', () => {46 assert.strictEqual(stringToBytes.parse('1tb'), 1 * Math.pow(1024, 4));47 assert.strictEqual(stringToBytes.parse('1TB'), 1 * Math.pow(1024, 4));48 assert.strictEqual(stringToBytes.parse('1Tb'), 1 * Math.pow(1024, 4));49 assert.strictEqual(stringToBytes.parse('1tB'), 1 * Math.pow(1024, 4));50 assert.strictEqual(stringToBytes.parse('0.5tb'), 0.5 * Math.pow(1024, 4));51 assert.strictEqual(stringToBytes.parse('0.5TB'), 0.5 * Math.pow(1024, 4));52 assert.strictEqual(stringToBytes.parse('0.5Tb'), 0.5 * Math.pow(1024, 4));53 assert.strictEqual(stringToBytes.parse('0.5tB'), 0.5 * Math.pow(1024, 4));54 assert.strictEqual(stringToBytes.parse('1.5tb'), 1.5 * Math.pow(1024, 4));55 assert.strictEqual(stringToBytes.parse('1.5TB'), 1.5 * Math.pow(1024, 4));56 assert.strictEqual(stringToBytes.parse('1.5Tb'), 1.5 * Math.pow(1024, 4));57 assert.strictEqual(stringToBytes.parse('1.5tB'), 1.5 * Math.pow(1024, 4));58 });59 it('Should parse PB', () => {60 assert.strictEqual(stringToBytes.parse('1pb'), 1 * Math.pow(1024, 5));61 assert.strictEqual(stringToBytes.parse('1PB'), 1 * Math.pow(1024, 5));62 assert.strictEqual(stringToBytes.parse('1Pb'), 1 * Math.pow(1024, 5));63 assert.strictEqual(stringToBytes.parse('1pB'), 1 * Math.pow(1024, 5));64 assert.strictEqual(stringToBytes.parse('0.5pb'), 0.5 * Math.pow(1024, 5));65 assert.strictEqual(stringToBytes.parse('0.5PB'), 0.5 * Math.pow(1024, 5));66 assert.strictEqual(stringToBytes.parse('0.5Pb'), 0.5 * Math.pow(1024, 5));67 assert.strictEqual(stringToBytes.parse('0.5pB'), 0.5 * Math.pow(1024, 5));68 assert.strictEqual(stringToBytes.parse('1.5pb'), 1.5 * Math.pow(1024, 5));69 assert.strictEqual(stringToBytes.parse('1.5PB'), 1.5 * Math.pow(1024, 5));70 assert.strictEqual(stringToBytes.parse('1.5Pb'), 1.5 * Math.pow(1024, 5));71 assert.strictEqual(stringToBytes.parse('1.5pB'), 1.5 * Math.pow(1024, 5));72 });73 it('Should assume stringToBytes when no units', () => {74 assert.strictEqual(stringToBytes.parse('0'), 0);75 assert.strictEqual(stringToBytes.parse('-1'), -1);76 assert.strictEqual(stringToBytes.parse('1024'), 1024);77 assert.strictEqual(stringToBytes.parse('0x11'), 0);78 });79 it('Should accept negative values', () => {80 assert.strictEqual(stringToBytes.parse('-1'), -1);81 assert.strictEqual(stringToBytes.parse('-1024'), -1024);82 assert.strictEqual(stringToBytes.parse('-1.5TB'), -1.5 * Math.pow(1024, 4));83 });84 it('Should drop partial stringToBytes', () => {85 assert.strictEqual(stringToBytes.parse('1.1b'), 1);86 assert.strictEqual(stringToBytes.parse('1.0001kb'), 1024);87 });88 it('Should allow whitespace', () => {89 assert.strictEqual(stringToBytes.parse('1 TB'), 1 * Math.pow(1024, 4));90 });...

Full Screen

Full Screen

stringToBytes.test.ts

Source:stringToBytes.test.ts Github

copy

Full Screen

...6 */7import stringToBytes from '../stringToBytes';8describe('numeric input', () => {9 test('> 1 represents bytes', () => {10 expect(stringToBytes(50.8)).toEqual(50);11 });12 test('1.1 should be a 1', () => {13 expect(stringToBytes(1.1, 54)).toEqual(1);14 });15 test('< 1 represents a %', () => {16 expect(stringToBytes(0.3, 51)).toEqual(15);17 });18 test('should throw when no reference supplied', () => {19 expect(() => stringToBytes(0.3)).toThrowError();20 });21 test('should throw on a bad input', () => {22 expect(() => stringToBytes(-0.3, 51)).toThrowError();23 });24});25describe('string input', () => {26 describe('numeric passthrough', () => {27 test('> 1 represents bytes', () => {28 expect(stringToBytes('50.8')).toEqual(50);29 });30 test('< 1 represents a %', () => {31 expect(stringToBytes('0.3', 51)).toEqual(15);32 });33 test('should throw when no reference supplied', () => {34 expect(() => stringToBytes('0.3')).toThrowError();35 });36 test('should throw on a bad input', () => {37 expect(() => stringToBytes('-0.3', 51)).toThrowError();38 });39 });40 describe('parsing', () => {41 test('0% should throw an error', () => {42 expect(() => stringToBytes('0%', 51)).toThrowError();43 });44 test('30%', () => {45 expect(stringToBytes('30%', 51)).toEqual(15);46 });47 test('80%', () => {48 expect(stringToBytes('80%', 51)).toEqual(40);49 });50 test('100%', () => {51 expect(stringToBytes('100%', 51)).toEqual(51);52 });53 // The units caps is intentionally janky to test for forgiving string parsing.54 describe('k', () => {55 test('30k', () => {56 expect(stringToBytes('30K')).toEqual(30000);57 });58 test('30KB', () => {59 expect(stringToBytes('30kB')).toEqual(30000);60 });61 test('30KiB', () => {62 expect(stringToBytes('30kIb')).toEqual(30720);63 });64 });65 describe('m', () => {66 test('30M', () => {67 expect(stringToBytes('30M')).toEqual(30000000);68 });69 test('30MB', () => {70 expect(stringToBytes('30MB')).toEqual(30000000);71 });72 test('30MiB', () => {73 expect(stringToBytes('30MiB')).toEqual(31457280);74 });75 });76 describe('g', () => {77 test('30G', () => {78 expect(stringToBytes('30G')).toEqual(30000000000);79 });80 test('30GB', () => {81 expect(stringToBytes('30gB')).toEqual(30000000000);82 });83 test('30GiB', () => {84 expect(stringToBytes('30GIB')).toEqual(32212254720);85 });86 });87 test('unknown unit', () => {88 expect(() => stringToBytes('50XX')).toThrowError();89 });90 });91});92test('nesting', () => {93 expect(stringToBytes(stringToBytes(stringToBytes('30%', 51)))).toEqual(15);94});95test('null', () => {96 expect(stringToBytes(null)).toEqual(null);97});98test('undefined', () => {99 expect(stringToBytes(undefined)).toEqual(undefined);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var str = 'hello world';3var bytes = wptools.stringToBytes(str);4console.log(bytes);5var wptools = require('wptools');6var bytes = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];7var str = wptools.bytesToString(bytes);8console.log(str);9var wptools = require('wptools');10var hex = '68656c6c6f20776f726c64';11var bytes = wptools.hexToBytes(hex);12console.log(bytes);13var wptools = require('wptools');14var bytes = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];15var hex = wptools.bytesToHex(bytes);16console.log(hex);17var wptools = require('wptools');18var base64 = 'aGVsbG8gd29ybGQ=';19var bytes = wptools.base64ToBytes(base64);20console.log(bytes);21var wptools = require('wptools');22var bytes = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];23var base64 = wptools.bytesToBase64(bytes);24console.log(base64);25var wptools = require('wptools');26var base64 = 'aGVsbG8gd29ybGQ=';27var bytes = wptools.base64ToBytes(base64);28var base64 = wptools.bytesToBase64(bytes);29console.log(base64);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var wiki = new wptools('Albert_Einstein');3wiki.get(function(err, resp) {4 if (err) {5 console.log(err);6 } else {7 console.log(resp);8 }9});10var wptools = require('wptools');11var wiki = new wptools('Albert_Einstein');12wiki.get(function(err, resp) {13 if (err) {14 console.log(err);15 } else {16 console.log(resp);17 }18});

Full Screen

Using AI Code Generation

copy

Full Screen

1var str = "Hello World";2var arr = stringToBytes(str);3console.log(arr);4var arr = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100];5var str = bytesToString(arr);6console.log(str);7var str = "Hello World";8var hex = stringToHex(str);9console.log(hex);10var hex = "48656c6c6f20576f726c64";11var str = hexToString(hex);12console.log(str);13var str = "Hello World";14var base64 = stringToBase64(str);15console.log(base64);16var base64 = "SGVsbG8gV29ybGQ=";17var str = base64ToString(base64);18console.log(str);19var str = "Hello World";20var utf8 = stringToUtf8(str);21console.log(utf8);22var utf8 = "48656c6c6f20576f726c64";23var str = utf8ToString(utf8);24console.log(str);

Full Screen

Using AI Code Generation

copy

Full Screen

1const wptools = require('wptools');2const str = 'hello world';3const buffer = wptools.stringToBytes(str);4console.log(buffer);5### stringToBytes(str)6### bytesToString(buffer)7MIT © [Anshul](

Full Screen

Using AI Code Generation

copy

Full Screen

1const wptools = require('wptools');2var str = "Hello World!";3var bytes = wptools.stringToBytes(str);4console.log(bytes);5#### stringToBytes(str)6#### bytesToString(bytes)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('../index.js');2var string = "Hello World";3var bytes = wptools.stringToBytes(string);4console.log(bytes);5console.log(wptools.bytesToString(bytes));6var wptools = require('../index.js');7var string = "Hello World";8var bytes = wptools.stringToBytes(string);9console.log(bytes);10console.log(wptools.bytesToString(bytes));11[MIT](LICENSE) © [Kashish Grover](

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful