TypeScript and JavaScript to Ruby
Recorder: the Node adapter (modules are imported; TypeScript is transpiled first). Frameworks:
Express, Fastify, Koa, NestJS routes are found by analyze; React/Vue/Angular/Svelte/Next.js pages
become ERB views (read frontend.md).
Constructs
- Errors:
throw new Error(m)->raise m(RuntimeError);TypeError->TypeError;RangeError->RangeError(or ArgumentError); customclass X extends Error->class X < StandardError. A rejected promise is a raise. - null / undefined ->
nil. An object key holdingundefineddisappears fromJSON.stringify;nilvalues stay asnullin Rails'render json:. Usecompactwhere the original relied on undefined keys vanishing. - Numbers: one
numbertype. Integral results come back as ints in recordings; fractional ones as floats.Number.isInteger(5.0)is true. ReturnIntegerwhere the recording shows ints. - Truthiness:
0,"",NaN,null,undefinedare false in JS; onlynil/falsein Ruby.x || default->x.presence || defaultfor strings, explicit zero checks for numbers.??->x.nil? ? default : x. - Arrays:
map/filter/reduce/find/some/every/includes->map/select/reduce/find/any?/all?/include?.arr.sort()without a comparator sorts as strings ([10, 9, 1].sort() == [1, 10, 9]): port that assort_by(&:to_s).sort((a, b) => a - b)->sort. - Objects keep insertion order, except integer-like keys, which JS puts first in ascending
order. Spread
{...a, ...b}->a.merge(b). Optional chaininga?.b->a&.dig(:b)/&.. - Strings:
lengthand indexes count UTF-16 code units: an emoji is 2. Ruby counts code points. Where emoji or astral characters reach a length, slice or index, recorded cases will show it; uses.encode("UTF-16LE").bytesize / 2for length parity.substringswaps reversed arguments;slicetakes negatives.toUpperCaseis locale-free likeupcase. - Express:
req.params/req.query/req.body->params;res.status(201).json(x)->render json: x, status: :created;res.redirect(path)->redirect_to path(302 in both); middleware ->before_action/ Rack middleware;express.static->public/. - Async:
awaitchains become plain sequential calls;Promise.allover I/O -> sequential, or threads only when an ADR says latency matters. - Mongoose / Prisma / Sequelize / TypeORM -> ActiveRecord models; Mongo documents need a schema decision (ADR) or the mongoid gem.
Traps
- Rounding:
Math.round(-2.5) == -2(half toward +infinity); Ruby-2.5.round == -3. Use(x + 0.5).floor.toFixed(2)rounds the binary value:(1.005).toFixed(2) == "1.00"; Rubyformat("%.2f", 1.005) == "1.00"too, but1.005.round(2) == 1.01: useformat. - Integer division:
7 / 2 == 3.5;Math.floor(7 / 2)floors,Math.trunctruncates (negative numbers differ).%takes the dividend's sign (-7 % 2 == -1); Ruby takes the divisor's (1): useremainder. - Regex: no
uflag means\wis ASCII (same as Ruby)./gwith.test()keepslastIndexstate between calls: port the bug only if recorded. - Dates:
new Date(y, m, d)months start at 0;Date.parseof a date-only string is UTC. - parseInt("12px") == 12; Ruby
Integer("12px")raises: use"12px".to_i(butto_ireturns 0 whereparseIntreturnsNaN). ==coercion ("1" == 1): port the coercion explicitly where a recorded case depends on it.