Skip to content

Commit 2227d79

Browse files
authored
Merge pull request #81 from githubnext/autoloop/build-tsb-pandas-typescript-migration-iter136-3d86b3aeb7079b68
[Autoloop] build-tsb-pandas-typescript-migration: Iteration 136 — toDictOriented/fromDictOriented
2 parents 7fefad0 + 92e747b commit 2227d79

54 files changed

Lines changed: 17051 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

playground/api_types.html

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>tsb — api_types: Runtime type-checking predicates</title>
7+
<style>
8+
body { font-family: system-ui, sans-serif; max-width: 860px; margin: 2rem auto; padding: 0 1rem; line-height: 1.6; }
9+
h1 { border-bottom: 2px solid #e2e8f0; padding-bottom: .5rem; }
10+
h2 { margin-top: 2rem; color: #2d3748; }
11+
h3 { color: #4a5568; margin-top: 1.5rem; }
12+
pre { background: #f7fafc; border: 1px solid #e2e8f0; border-radius: 6px; padding: 1rem; overflow-x: auto; font-size: .9rem; }
13+
code { font-family: 'Fira Code', monospace; }
14+
.result { background: #ebf8ff; border-left: 4px solid #4299e1; padding: .5rem 1rem; margin: .5rem 0; border-radius: 0 6px 6px 0; font-family: monospace; }
15+
.ok { color: #276749; }
16+
.fail { color: #c53030; }
17+
.section { background: #fff5f5; border: 1px solid #fed7d7; border-radius: 6px; padding: 1rem; margin: 1rem 0; }
18+
table { border-collapse: collapse; width: 100%; margin: 1rem 0; }
19+
th, td { border: 1px solid #e2e8f0; padding: .4rem .8rem; text-align: left; }
20+
th { background: #f7fafc; font-weight: 600; }
21+
a { color: #3182ce; }
22+
</style>
23+
</head>
24+
<body>
25+
<h1>📦 <code>api_types</code> — Runtime type-checking predicates</h1>
26+
<p>
27+
Port of <a href="https://pandas.pydata.org/docs/reference/api/pandas.api.types.html" target="_blank"><code>pandas.api.types</code></a>.
28+
Two groups of predicates:
29+
<strong>value-level</strong> (work on arbitrary JS values) and
30+
<strong>dtype-level</strong> (work on <code>Dtype</code> instances or dtype name strings).
31+
</p>
32+
33+
<h2>Value-Level Predicates</h2>
34+
35+
<h3><code>isScalar(val)</code></h3>
36+
<p>Returns <code>true</code> for primitives and <code>Date</code>. Mirrors <code>pd.api.types.is_scalar</code>.</p>
37+
<pre><code>import { isScalar } from "tsb";
38+
39+
isScalar(42); // true
40+
isScalar("hello"); // true
41+
isScalar(null); // true
42+
isScalar(new Date()); // true
43+
isScalar([1, 2]); // false
44+
isScalar({ a: 1 }); // false</code></pre>
45+
<div id="scalar-demo" class="result"></div>
46+
47+
<h3><code>isListLike(val)</code></h3>
48+
<p>Returns <code>true</code> for iterables (excluding strings) and objects with a numeric <code>length</code>.</p>
49+
<pre><code>isListLike([1, 2, 3]); // true
50+
isListLike(new Set([1])); // true
51+
isListLike("abc"); // false
52+
isListLike(42); // false</code></pre>
53+
<div id="listlike-demo" class="result"></div>
54+
55+
<h3><code>isArrayLike(val)</code></h3>
56+
<p>Returns <code>true</code> for values with a non-negative integer <code>length</code> (including strings).</p>
57+
<pre><code>isArrayLike([1, 2]); // true
58+
isArrayLike("hello"); // true
59+
isArrayLike(42); // false</code></pre>
60+
61+
<h3><code>isDictLike(val)</code></h3>
62+
<p>Returns <code>true</code> for plain objects and <code>Map</code>.</p>
63+
<pre><code>isDictLike({ a: 1 }); // true
64+
isDictLike(new Map()); // true
65+
isDictLike([]); // false</code></pre>
66+
67+
<h3><code>isNumber / isBool / isStringValue / isFloat / isInteger</code></h3>
68+
<pre><code>isNumber(3.14); // true
69+
isNumber(NaN); // true (typeof NaN === "number")
70+
isBool(true); // true
71+
isStringValue("hi"); // true
72+
isFloat(3.14); // true
73+
isFloat(3.0); // false (integer value)
74+
isInteger(42); // true
75+
isInteger(3.14); // false</code></pre>
76+
<div id="value-demo" class="result"></div>
77+
78+
<h3><code>isMissing(val)</code></h3>
79+
<p>Returns <code>true</code> for <code>null</code>, <code>undefined</code>, or <code>NaN</code>.</p>
80+
<pre><code>isMissing(null); // true
81+
isMissing(undefined); // true
82+
isMissing(NaN); // true
83+
isMissing(0); // false</code></pre>
84+
85+
<h3><code>isHashable(val)</code></h3>
86+
<p>Returns <code>true</code> for values safe to use as object keys (primitives).</p>
87+
<pre><code>isHashable("key"); // true
88+
isHashable(42); // true
89+
isHashable({}); // false</code></pre>
90+
91+
<h2>Dtype-Level Predicates</h2>
92+
<p>All accept a <code>Dtype</code> instance or a dtype name string.</p>
93+
94+
<pre><code>import { Dtype, isNumericDtype, isFloatDtype, isIntegerDtype,
95+
isStringDtype, isDatetimeDtype, isCategoricalDtype } from "tsb";
96+
97+
isNumericDtype(Dtype.float64); // true
98+
isNumericDtype("int32"); // true
99+
isNumericDtype("string"); // false
100+
101+
isFloatDtype("float32"); // true
102+
isIntegerDtype("int64"); // true
103+
isUnsignedIntegerDtype("uint8"); // true
104+
isSignedIntegerDtype("int8"); // true
105+
isStringDtype("string"); // true
106+
isDatetimeDtype("datetime"); // true
107+
isCategoricalDtype("category"); // true
108+
isObjectDtype("object"); // true
109+
isExtensionArrayDtype("category"); // true
110+
isExtensionArrayDtype("int32"); // false</code></pre>
111+
<div id="dtype-demo" class="result"></div>
112+
113+
<h2>Complete Predicate Reference</h2>
114+
<table>
115+
<tr><th>Function</th><th>Pandas equivalent</th><th>Description</th></tr>
116+
<tr><td><code>isScalar(val)</code></td><td><code>is_scalar</code></td><td>Primitive or Date</td></tr>
117+
<tr><td><code>isListLike(val)</code></td><td><code>is_list_like</code></td><td>Iterable (not string) or has length</td></tr>
118+
<tr><td><code>isArrayLike(val)</code></td><td><code>is_array_like</code></td><td>Has non-negative integer length</td></tr>
119+
<tr><td><code>isDictLike(val)</code></td><td><code>is_dict_like</code></td><td>Plain object or Map</td></tr>
120+
<tr><td><code>isIterator(val)</code></td><td><code>is_iterator</code></td><td>Has callable <code>next</code> method</td></tr>
121+
<tr><td><code>isNumber(val)</code></td><td><code>is_number</code></td><td><code>typeof === "number"</code></td></tr>
122+
<tr><td><code>isBool(val)</code></td><td><code>is_bool</code></td><td><code>typeof === "boolean"</code></td></tr>
123+
<tr><td><code>isStringValue(val)</code></td><td><code>is_string</code></td><td><code>typeof === "string"</code></td></tr>
124+
<tr><td><code>isFloat(val)</code></td><td><code>is_float</code></td><td>Finite number with fractional part</td></tr>
125+
<tr><td><code>isInteger(val)</code></td><td><code>is_integer</code></td><td>Integer-valued number</td></tr>
126+
<tr><td><code>isBigInt(val)</code></td><td></td><td><code>typeof === "bigint"</code></td></tr>
127+
<tr><td><code>isRegExp(val)</code></td><td><code>is_re</code></td><td>RegExp instance</td></tr>
128+
<tr><td><code>isReCompilable(val)</code></td><td><code>is_re_compilable</code></td><td>String or RegExp</td></tr>
129+
<tr><td><code>isMissing(val)</code></td><td><code>isna</code></td><td>null / undefined / NaN</td></tr>
130+
<tr><td><code>isHashable(val)</code></td><td><code>is_hashable</code></td><td>Safe as object key (primitive)</td></tr>
131+
<tr><td><code>isDate(val)</code></td><td></td><td>Date instance</td></tr>
132+
<tr><td><code>isNumericDtype(d)</code></td><td><code>is_numeric_dtype</code></td><td>Int, uint, or float</td></tr>
133+
<tr><td><code>isIntegerDtype(d)</code></td><td><code>is_integer_dtype</code></td><td>Any integer (signed or unsigned)</td></tr>
134+
<tr><td><code>isSignedIntegerDtype(d)</code></td><td><code>is_signed_integer_dtype</code></td><td>int8–int64</td></tr>
135+
<tr><td><code>isUnsignedIntegerDtype(d)</code></td><td><code>is_unsigned_integer_dtype</code></td><td>uint8–uint64</td></tr>
136+
<tr><td><code>isFloatDtype(d)</code></td><td><code>is_float_dtype</code></td><td>float32 or float64</td></tr>
137+
<tr><td><code>isBoolDtype(d)</code></td><td><code>is_bool_dtype</code></td><td>bool</td></tr>
138+
<tr><td><code>isStringDtype(d)</code></td><td><code>is_string_dtype</code></td><td>string dtype</td></tr>
139+
<tr><td><code>isDatetimeDtype(d)</code></td><td><code>is_datetime64_dtype</code></td><td>datetime</td></tr>
140+
<tr><td><code>isTimedeltaDtype(d)</code></td><td><code>is_timedelta64_dtype</code></td><td>timedelta</td></tr>
141+
<tr><td><code>isCategoricalDtype(d)</code></td><td><code>is_categorical_dtype</code></td><td>category</td></tr>
142+
<tr><td><code>isObjectDtype(d)</code></td><td><code>is_object_dtype</code></td><td>object</td></tr>
143+
<tr><td><code>isComplexDtype(d)</code></td><td><code>is_complex_dtype</code></td><td>Always false (no complex in tsb)</td></tr>
144+
<tr><td><code>isExtensionArrayDtype(d)</code></td><td><code>is_extension_array_dtype</code></td><td>string/object/datetime/timedelta/category</td></tr>
145+
<tr><td><code>isPeriodDtype(d)</code></td><td><code>is_period_dtype</code></td><td>Maps to datetime</td></tr>
146+
<tr><td><code>isIntervalDtype(d)</code></td><td><code>is_interval_dtype</code></td><td>Numeric dtypes</td></tr>
147+
</table>
148+
149+
<script type="module">
150+
// Inline demo (no bundler required for playground)
151+
function show(id, lines) {
152+
document.getElementById(id).innerHTML = lines
153+
.map(([expr, val]) => `<span class="${val ? 'ok' : 'fail'}">${expr}: ${val}</span>`)
154+
.join('<br>');
155+
}
156+
157+
// Value predicate demos (inline implementations for playground)
158+
function isScalar(val) {
159+
if (val === null || val === undefined) return true;
160+
const t = typeof val;
161+
if (t === "string" || t === "number" || t === "bigint" || t === "boolean" || t === "symbol") return true;
162+
if (val instanceof Date) return true;
163+
return false;
164+
}
165+
function isListLike(val) {
166+
if (val === null || val === undefined || typeof val === "string") return false;
167+
if (typeof val === "number" || typeof val === "boolean") return false;
168+
if (typeof val === "object") {
169+
if (Symbol.iterator in val) return true;
170+
const len = val["length"];
171+
if (typeof len === "number" && len >= 0 && Number.isInteger(len)) return true;
172+
}
173+
return false;
174+
}
175+
176+
show("scalar-demo", [
177+
["isScalar(42)", isScalar(42)],
178+
["isScalar('hello')", isScalar("hello")],
179+
["isScalar(null)", isScalar(null)],
180+
["isScalar([1, 2])", isScalar([1, 2])],
181+
["isScalar({ a: 1 })", isScalar({ a: 1 })],
182+
]);
183+
184+
show("listlike-demo", [
185+
["isListLike([1, 2, 3])", isListLike([1, 2, 3])],
186+
["isListLike(new Set([1]))", isListLike(new Set([1]))],
187+
["isListLike('abc')", isListLike("abc")],
188+
["isListLike(42)", isListLike(42)],
189+
]);
190+
191+
function isFloat(val) {
192+
if (typeof val !== "number" || !Number.isFinite(val)) return false;
193+
return val !== Math.trunc(val);
194+
}
195+
function isInteger(val) { return typeof val === "number" && Number.isInteger(val); }
196+
197+
show("value-demo", [
198+
["isNumber(3.14)", typeof 3.14 === "number"],
199+
["isNumber(NaN)", typeof NaN === "number"],
200+
["isBool(true)", typeof true === "boolean"],
201+
["isFloat(3.14)", isFloat(3.14)],
202+
["isFloat(3.0)", isFloat(3.0)],
203+
["isInteger(42)", isInteger(42)],
204+
["isInteger(3.14)", isInteger(3.14)],
205+
]);
206+
207+
// Dtype demo table output
208+
const dtypes = {
209+
isNumeric: name => ["int8","int16","int32","int64","uint8","uint16","uint32","uint64","float32","float64"].includes(name),
210+
isFloat: name => ["float32","float64"].includes(name),
211+
isInteger: name => ["int8","int16","int32","int64","uint8","uint16","uint32","uint64"].includes(name),
212+
};
213+
show("dtype-demo", [
214+
["isNumericDtype('float64')", dtypes.isNumeric("float64")],
215+
["isNumericDtype('int32')", dtypes.isNumeric("int32")],
216+
["isNumericDtype('string')", dtypes.isNumeric("string")],
217+
["isFloatDtype('float32')", dtypes.isFloat("float32")],
218+
["isIntegerDtype('int64')", dtypes.isInteger("int64")],
219+
]);
220+
</script>
221+
</body>
222+
</html>

0 commit comments

Comments
 (0)