@applitools/dom-shared
Advanced tools
Comparing version 1.0.0 to 1.0.2
12
index.js
@@ -1,3 +0,9 @@ | ||
exports.chunkify = require('./src/chunkify'); | ||
exports.pollify = require('./src/pollify'); | ||
exports.poll = require('./src/poll'); | ||
const chunkify = require('./src/chunkify'); | ||
const pollify = require('./src/pollify'); | ||
const poll = require('./src/poll'); | ||
module.exports = { | ||
chunkify, | ||
pollify, | ||
poll, | ||
}; |
{ | ||
"name": "@applitools/dom-shared", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"main": "index.js", | ||
@@ -8,7 +8,5 @@ "license": "SEE LICENSE IN LICENSE", | ||
"test": "mocha --no-timeouts 'test/**/*.test.js'", | ||
"eslint:node": "eslint '**/*.js' --ignore-pattern 'src/browser'", | ||
"eslint:browser": "eslint -c 'src/browser/.eslintrc.json' 'src/browser/**/*.js'", | ||
"lint": "yarn eslint:node && yarn eslint:browser", | ||
"lint": "eslint '**/*.js'", | ||
"preversion": "BONGO_SKIP_VERIFY_VERSIONS=1 BONGO_SKIP_VERIFY_COMMITS=1 BONGO_SKIP_VERIFY_INSTALLED_VERSIONS=1 bongo preversion && yarn test", | ||
"version": "yarn build && bongo version", | ||
"version": "bongo version", | ||
"postversion": "bongo postversion --skip-release-notification" | ||
@@ -25,8 +23,12 @@ }, | ||
"@applitools/eslint-plugin-compat": "^0.7.0", | ||
"@applitools/functional-commons": "^1.6.0", | ||
"@applitools/monitoring-commons": "^1.0.19", | ||
"@applitools/sdk-release-kit": "^0.2.2", | ||
"mocha": "^8.1.3" | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-mocha-no-only": "1.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"mocha": "^8.1.3", | ||
"prettier": "^1.19.1" | ||
}, | ||
"dependencies": {} | ||
} |
function charCodeAt(string, index = 0) { | ||
const code = string.charCodeAt(index) | ||
const code = string.charCodeAt(index); | ||
if (0xd800 <= code && code < 0xdc00) { | ||
const high = code | ||
const low = string.charCodeAt(index + 1) | ||
const high = code; | ||
const low = string.charCodeAt(index + 1); | ||
return (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000 | ||
return (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000; | ||
} | ||
if (0xdc00 <= code && code <= 0xdfff) return -1 | ||
return code | ||
if (0xdc00 <= code && code <= 0xdfff) return -1; | ||
return code; | ||
} | ||
function chunkify(string, chunkByteLength) { | ||
const chunks = [] | ||
let currChunkByteLength = 0 | ||
let lastChunkIndex = 0 | ||
const chunks = []; | ||
let currChunkByteLength = 0; | ||
let lastChunkIndex = 0; | ||
for (let index = 0; index < string.length; ++index) { | ||
const code = charCodeAt(string, index) | ||
let charByteLength = 0 | ||
const code = charCodeAt(string, index); | ||
let charByteLength = 0; | ||
if (code > 0) { | ||
if (code < 128) charByteLength = 1 | ||
else if (code < 2048) charByteLength = 2 | ||
else if (code < 65536) charByteLength = 3 | ||
else if (code < 2097152) charByteLength = 4 | ||
else if (code < 67108864) charByteLength = 5 | ||
else charByteLength = 6 | ||
if (code < 128) charByteLength = 1; | ||
else if (code < 2048) charByteLength = 2; | ||
else if (code < 65536) charByteLength = 3; | ||
else if (code < 2097152) charByteLength = 4; | ||
else if (code < 67108864) charByteLength = 5; | ||
else charByteLength = 6; | ||
} | ||
if (currChunkByteLength + charByteLength > chunkByteLength) { | ||
chunks.push(string.substring(lastChunkIndex, index)) | ||
lastChunkIndex = index | ||
currChunkByteLength = charByteLength | ||
chunks.push(string.substring(lastChunkIndex, index)); | ||
lastChunkIndex = index; | ||
currChunkByteLength = charByteLength; | ||
} else { | ||
currChunkByteLength += charByteLength | ||
currChunkByteLength += charByteLength; | ||
} | ||
} | ||
chunks.push(string.substring(lastChunkIndex)) | ||
chunks.push(string.substring(lastChunkIndex)); | ||
return chunks | ||
return chunks; | ||
} | ||
module.exports = chunkify | ||
module.exports = chunkify; |
@@ -1,2 +0,2 @@ | ||
const chunkify = require('./chunkify') | ||
const chunkify = require('./chunkify'); | ||
@@ -8,4 +8,4 @@ const STATUSES = { | ||
ERROR: 'ERROR', | ||
} | ||
const DEFAULT_CHUNK_BYTE_LENGTH = 268435456 // 256MB | ||
}; | ||
const DEFAULT_CHUNK_BYTE_LENGTH = 268435456; // 256MB | ||
@@ -17,8 +17,8 @@ function createPollResponse(state, {chunkByteLength = DEFAULT_CHUNK_BYTE_LENGTH} = {}) { | ||
error: 'unexpected poll request received - cannot find state of current operation', | ||
} | ||
}; | ||
} else if (state.value) { | ||
if (chunkByteLength) { | ||
if (!state.chunks) { | ||
state.chunks = chunkify(JSON.stringify(state.value), chunkByteLength) | ||
state.splitted = state.chunks.length > 1 | ||
state.chunks = chunkify(JSON.stringify(state.value), chunkByteLength); | ||
state.splitted = state.chunks.length > 1; | ||
} | ||
@@ -30,10 +30,10 @@ if (state.splitted) { | ||
done: state.chunks.length === 0, | ||
} | ||
}; | ||
} | ||
} | ||
return {status: STATUSES.SUCCESS, value: state.value} | ||
return {status: STATUSES.SUCCESS, value: state.value}; | ||
} else if (state.error) { | ||
return {status: STATUSES.ERROR, error: state.error} | ||
return {status: STATUSES.ERROR, error: state.error}; | ||
} else { | ||
return {status: STATUSES.WIP} | ||
return {status: STATUSES.WIP}; | ||
} | ||
@@ -43,4 +43,4 @@ } | ||
function poll(context, key, options = {}) { | ||
context = context || {} | ||
const result = createPollResponse(context[key], options) | ||
context = context || {}; | ||
const result = createPollResponse(context[key], options); | ||
@@ -52,8 +52,8 @@ if ( | ||
) { | ||
context[key] = null | ||
context[key] = null; | ||
} | ||
return result | ||
return result; | ||
} | ||
module.exports = poll | ||
module.exports = poll; |
@@ -1,15 +0,15 @@ | ||
const poll = require('./poll') | ||
const poll = require('./poll'); | ||
function pollify(script, context = {}, key = 'state') { | ||
return function(doc, options) { | ||
return options => (...args) => { | ||
if (!context[key]) { | ||
context[key] = {} | ||
script(doc, options) | ||
context[key] = {}; | ||
script(args) | ||
.then(value => (context[key].value = value)) | ||
.catch(err => (context[key].error = err.message)) | ||
.catch(err => (context[key].error = err.message)); | ||
} | ||
return poll(context, key, options) | ||
} | ||
return poll(context, key, options); | ||
}; | ||
} | ||
module.exports = pollify | ||
module.exports = pollify; |
@@ -1,22 +0,22 @@ | ||
const assert = require('assert') | ||
const chunkify = require('../src/chunkify') | ||
const assert = require('assert'); | ||
const chunkify = require('../src/chunkify'); | ||
describe('chunkify', () => { | ||
it('works with latin', async () => { | ||
const chunks = chunkify('abcdefgh1234', 4) | ||
assert.strictEqual(chunks.length, 3) | ||
assert.deepStrictEqual(chunks, ['abcd', 'efgh', '1234']) | ||
}) | ||
const chunks = chunkify('abcdefgh1234', 4); | ||
assert.strictEqual(chunks.length, 3); | ||
assert.deepStrictEqual(chunks, ['abcd', 'efgh', '1234']); | ||
}); | ||
it('works with cyrillic', async () => { | ||
const chunks = chunkify('абвгґдеє1234', 4) | ||
assert.strictEqual(chunks.length, 5) | ||
assert.deepStrictEqual(chunks, ['аб', 'вг', 'ґд', 'еє', '1234']) | ||
}) | ||
const chunks = chunkify('абвгґдеє1234', 4); | ||
assert.strictEqual(chunks.length, 5); | ||
assert.deepStrictEqual(chunks, ['аб', 'вг', 'ґд', 'еє', '1234']); | ||
}); | ||
it('works with emoji', async () => { | ||
const chunks = chunkify('😊🥺😉😍', 4) | ||
assert.strictEqual(chunks.length, 4) | ||
assert.deepStrictEqual(chunks, ['😊', '🥺', '😉', '😍']) | ||
}) | ||
}) | ||
const chunks = chunkify('😊🥺😉😍', 4); | ||
assert.strictEqual(chunks.length, 4); | ||
assert.deepStrictEqual(chunks, ['😊', '🥺', '😉', '😍']); | ||
}); | ||
}); |
@@ -1,42 +0,52 @@ | ||
const assert = require('assert') | ||
const poll = require('../src/poll') | ||
const assert = require('assert'); | ||
const poll = require('../src/poll'); | ||
describe('poll', () => { | ||
it('works', async () => { | ||
const key = 'key' | ||
const context = {[key]: {}} | ||
const r = poll(context, key) | ||
assert.deepStrictEqual(r, {status: 'WIP'}) | ||
context[key].value = 'result' | ||
const r2 = poll(context, key) | ||
assert.deepStrictEqual(r2, {status: 'SUCCESS', value: 'result'}) | ||
}) | ||
const key = 'key'; | ||
const context = {[key]: {}}; | ||
const r = poll(context, key); | ||
assert.deepStrictEqual(r, {status: 'WIP'}); | ||
context[key].value = 'result'; | ||
const r2 = poll(context, key); | ||
assert.deepStrictEqual(r2, {status: 'SUCCESS', value: 'result'}); | ||
}); | ||
it('returns result by chunks', async () => { | ||
const key = 'key' | ||
const context = {[key]: {}} | ||
const r = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r, {status: 'WIP'}) | ||
context[key].value = '😊12345єїжtrdh' | ||
const r2 = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r2, {status: 'SUCCESS_CHUNKED', value: '"😊', done: false}) | ||
const r3 = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r3, {status: 'SUCCESS_CHUNKED', value: '12345', done: false}) | ||
const r4 = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r4, {status: 'SUCCESS_CHUNKED', value: 'єї', done: false}) | ||
const r5 = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r5, {status: 'SUCCESS_CHUNKED', value: 'жtrd', done: false}) | ||
const r6 = poll(context, key, {chunkByteLength: 5}) | ||
assert.deepStrictEqual(r6, {status: 'SUCCESS_CHUNKED', value: 'h"', done: true}) | ||
}) | ||
const key = 'key'; | ||
const context = {[key]: {}}; | ||
const r = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r, {status: 'WIP'}); | ||
context[key].value = '😊12345єїжtrdh'; | ||
const r2 = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r2, {status: 'SUCCESS_CHUNKED', value: '"😊', done: false}); | ||
const r3 = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r3, {status: 'SUCCESS_CHUNKED', value: '12345', done: false}); | ||
const r4 = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r4, {status: 'SUCCESS_CHUNKED', value: 'єї', done: false}); | ||
const r5 = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r5, {status: 'SUCCESS_CHUNKED', value: 'жtrd', done: false}); | ||
const r6 = poll(context, key, {chunkByteLength: 5}); | ||
assert.deepStrictEqual(r6, {status: 'SUCCESS_CHUNKED', value: 'h"', done: true}); | ||
}); | ||
it('returns error when rejects', async () => { | ||
const key = 'key' | ||
const context = {[key]: {}} | ||
const r = poll(context, key) | ||
assert.deepStrictEqual(r, {status: 'WIP'}) | ||
context[key].error = 'error' | ||
const r2 = poll(context, key) | ||
assert.deepStrictEqual(r2, {status: 'ERROR', error: 'error'}) | ||
}) | ||
}) | ||
const key = 'key'; | ||
const context = {[key]: {}}; | ||
const r = poll(context, key); | ||
assert.deepStrictEqual(r, {status: 'WIP'}); | ||
context[key].error = 'error'; | ||
const r2 = poll(context, key); | ||
assert.deepStrictEqual(r2, {status: 'ERROR', error: 'error'}); | ||
}); | ||
it('returns error if no state', async () => { | ||
const key = 'key'; | ||
const context = {}; | ||
const r = poll(context, key); | ||
assert.deepStrictEqual(r, { | ||
status: 'ERROR', | ||
error: 'unexpected poll request received - cannot find state of current operation', | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24197
236
9
1