UNPKG

logiq

Version:

Awesome logical and bitwise operators with support for TypedArrays

41 lines (40 loc) 1.28 kB
import { makeConnectives } from './utils.js'; import { bitwise } from './index.js'; /** * Converts any `TypedArray` to a `Uint8Array`, preserving offset and length. */ function toBytes(p) { return new Uint8Array(p.buffer, p.byteOffset, p.byteLength); } /** * Applies a binary connective to two `TypedArrays` in bitwise fashion. */ function applyBitwise(connective, p, q) { // Work with bytes to avoid issues with non-integers and endianness const pBytes = toBytes(p); const qBytes = toBytes(q); if (pBytes.length === 0 || qBytes.length === 0) { throw new Error('Operands must have length > 0'); } const length = Math.max(pBytes.length, qBytes.length); const result = new Uint8Array(length); for (let i = 0; i < result.length; i++) { const pByte = pBytes[i % pBytes.length]; const qByte = qBytes[i % qBytes.length]; result[i] = connective(pByte, qByte); } return result; } function not(p) { return toBytes(p).map((value) => bitwise.not(value)); } function and(p, q) { return applyBitwise(bitwise.and, p, q); } function or(p, q) { return applyBitwise(bitwise.or, p, q); } function xor(p, q) { return applyBitwise(bitwise.xor, p, q); } export default makeConnectives({ not, and, or, xor });