ppu-paddle-ocr
Version:
Lightweight, probably the fastest PaddleOCR SDK in TypeScript. Runs anywhere JavaScript runs: Node.js, Bun, Deno, mobile react-native, web browsers, and browser extensions. Docker & CLI supported. The official SDK is browser-only. Accurate text detection
1 lines • 1.89 kB
JavaScript
export function deepMerge(target,...sources){if(!sources.length)return target;let source=sources.shift();if(isObject(target)&&isObject(source)){for(let key in source){if(Object.prototype.hasOwnProperty.call(source,key)){if(key==="__proto__"||key==="constructor"||key==="prototype"){continue}let sourceValue=source[key];let targetValue=target[key];if(isObject(sourceValue)){if(!targetValue||!isObject(targetValue)){target[key]={}}deepMerge(target[key],sourceValue)}else if(sourceValue!==undefined){target[key]=sourceValue}}}}return deepMerge(target,...sources)}export function levenshteinDistance(a,b){let m=a.length;let n=b.length;if(m===0)return n;if(n===0)return m;let prev=new Uint16Array(n+1);let curr=new Uint16Array(n+1);for(let j=0;j<=n;j++)prev[j]=j;for(let i=1;i<=m;i++){curr[0]=i;for(let j=1;j<=n;j++){let cost=a[i-1]===b[j-1]?0:1;curr[j]=Math.min((curr[j-1]??0)+1,(prev[j]??0)+1,(prev[j-1]??0)+cost)}let tmp=prev;prev=curr;curr=tmp}return prev[n]??0}export async function fetchArrayBufferWithRetry(url,options={}){const{timeoutMs=300000,retries=2}=options;let lastError;for(let attempt=0;attempt<=retries;attempt++){try{let response=await fetch(url,{signal:AbortSignal.timeout(timeoutMs)});if(!response.ok){throw new Error(`HTTP ${response.status} ${response.statusText}`)}return await response.arrayBuffer()}catch(error){lastError=error;if(attempt<retries){await new Promise((resolve)=>setTimeout(resolve,500*(attempt+1)))}}}throw new Error(`Failed to fetch ${url} after ${retries+1} attempt(s): ${String(lastError)}`)}export function parseDictionary(source){let content=typeof source==="string"?source:new TextDecoder("utf-8").decode(source);return content.split(/\r?\n/)}export function isObject(item){return item!==null&&typeof item==="object"&&!Array.isArray(item)&&!(item instanceof Date)&&!(item instanceof RegExp)&&!(item instanceof ArrayBuffer)&&!ArrayBuffer.isView(item)}