6 पॉइंट द्वारा honglu 2025-01-08 | 2 टिप्पणियां | WhatsApp पर शेयर करें

"चलो अपनी पसंद की type-safe लाइब्रेरी बनाते हैं" — इसी सोच के साथ यह प्रोजेक्ट शुरू हुआ।

यह प्रोजेक्ट type-safe JSON Schema इम्प्लीमेंटेशन से शुरू होकर, डेवलपमेंट प्रक्रिया में ज़रूरी विभिन्न टूल्स तक स्वाभाविक रूप से फैल गया।

फ़िलहाल नौकरी की तलाश के लिए मैंने पहले चरण में इसे एक विराम दिया है।

प्रोजेक्ट के सिद्धांत

इसे निम्नलिखित मुख्य सिद्धांतों का पालन करते हुए विकसित किया गया है:

  • सख्त type system का उपयोग
  • बाहरी dependencies को न्यूनतम रखना
  • पुन: उपयोग योग्य type system डिज़ाइन
  • API documentation
  • उच्च test coverage बनाए रखना
  • शुद्ध TypeScript इम्प्लीमेंटेशन

लाइब्रेरी

@imhonglu/json-schema

यह JSON Schema 2020-12 draft specification का पालन करने वाला TypeScript इम्प्लीमेंटेशन है।

  • रिपॉज़िटरी: https://github.com/imhonglu/new-wheels/…
  • JSON-Schema-Test-Suite के माध्यम से सत्यापन
  • schema definition के अनुसार उपलब्ध keywords के types अपने-आप infer हो जाते हैं।

[IMG] demo-1

import { Schema, SchemaDefinition } from "@imhonglu/json-schema";  
  
export const Address = new Schema({  
  type: "object",  
  properties: {  
    street: { type: "string" },  
    city: { type: "string" },  
    zip: { type: "string" },  
  },  
  required: ["street"] as const,  
});  
  
export type Address = SchemaDefinition.Instance<typeof Address>;  
// {  
//   street: string;  
//   city?: string;  
//   zip?: string;  
// }  

@imhonglu/format

यह प्रोजेक्ट JSON Schema के format keyword को इम्प्लीमेंट करने के लिए शुरू किया गया था।

  • रिपॉज़िटरी: https://github.com/imhonglu/new-wheels/…
  • RFC specification पर आधारित इम्प्लीमेंटेशन
  • JSON-Schema-Test-Suite आधारित सत्यापन
  • native JSON API जैसा interface प्रदान करता है
import { FullTime } from '@imhonglu/format';  
  
const time = FullTime.parse('00:00:00.000Z');  
// { hour: 0, minute: 0, second: 0, secfrac: '.000', offset: undefined }  
  
console.log(time.toString()); // '00:00:00.000Z'  
console.log(JSON.stringify(time)); // '"00:00:00.000Z"'  
  
const result = FullTime.safeParse('invalid');  
if (!result.ok) {  
  console.error(result.error);  
}  

@imhonglu/pattern-builder

यह एक regex builder है, जिसे RFC spec की ABNF grammar इम्प्लीमेंट करते समय regular expressions की readability बेहतर करने के लिए बनाया गया है।

import { characterSet, concat, hexDigit } from "@imhonglu/pattern-builder";  
  
// pct-encoded = "%" HEXDIG HEXDIG  
export const pctEncoded = concat(  
  "%",  
  hexDigit.clone().exact(2),  
);  
  
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"  
export const unreserved = characterSet(  
  alpha,  
  digit,  
  /[\-._~]/,  
);  

@imhonglu/type-guard

यह type guard लाइब्रेरी type guards की readability बेहतर करने के लिए बनाई गई है।

  • रिपॉज़िटरी: https://github.com/imhonglu/new-wheels/…
  • Proxy-आधारित इम्प्लीमेंटेशन से overhead को न्यूनतम किया गया है
  • not keyword उपलब्ध है
import { composeGuards } from "@imhonglu/type-guard";  
  
const is = composeGuards({  
  string: (value: unknown): value is string => typeof value === "string",  
  number: (value: unknown): value is number => typeof value === "number"  
});  
  
is.string("hello"); // true  
is.not.string(42);  // true  
  
let value: string | number | undefined;  
  
if (is.number(value)) {  
  value.toFixed(2); // 'value' is number  
}  
  
if (is.not.number(value)) {  
  value.toFixed(2); // error: Property 'toFixed' does not exist on type 'undefined'.  
}  

@imhonglu/type-object

यह native Object API की type-safe wrapper लाइब्रेरी है। यह native behavior के क़रीब types प्रदान करती है।

import * as TypeObject from '@imhonglu/type-object';  
  
const data = { a: 1, b: 2, c: 3 };  
for (const key of TypeObject.keys(data)) {  
  // key: "a" | "b" | "c"  
  console.log(data[key]); // number  
}  
  
const string = 'hello';  
for (const index of TypeObject.keys(string)) {  
  // index: number & keyof string  
  console.log(string[index]); // string  
}  

@imhonglu/toolkit

यह प्रोजेक्ट के भीतर उपयोग हो रहे utility types और utility functions का संग्रह है।

import type { Fn } from '@imhonglu/toolkit';  
  
// यह function type '(...args: any[]) => any' के लिए type alias प्रदान करता है।  
Fn.Callable // (...args: any[]) => any  
  
// generics के माध्यम से केवल argument types परिभाषित किए जा सकते हैं।  
Fn.Callable<{ args: [number, number] }> // (...args: [number, number]) => any  
  
// generics के माध्यम से केवल return type परिभाषित किया जा सकता है।  
Fn.Callable<{ return: string }> // (...args: any[]) => string  
  
// generics के माध्यम से argument types और return type दोनों परिभाषित किए जा सकते हैं।  
Fn.Callable<{ args: [number, number], return: string }> // (...args: [number, number]) => string  

आगे की योजना और नौकरी की तलाश

चल रहे प्रोजेक्ट का अगला चरण JSON Schema spec इम्प्लीमेंटेशन को पूरा करना है,
और मैं एक backend framework भी लिखकर देखना चाहता हूँ।

मैं इस समय नौकरी की तलाश में हूँ, इसलिए कृपया रुचि दिखाएँ।
पढ़ने के लिए धन्यवाद।

आपका दिन शुभ हो!

2 टिप्पणियां

 
jjpark78 2025-01-09

इस तरफ़ zod जैसा बेहतरीन विकल्प मौजूद है, इसलिए प्रोडक्ट में हम वही इस्तेमाल कर रहे हैं, लेकिन यह दिलचस्प है।

 
honglu 2025-01-09

ajv, typia, zod जैसे मौजूदा प्रोजेक्ट्स ऐसे प्रोजेक्ट हैं जिन पर मेरी भी काफ़ी दिलचस्पी है.

@imhonglu/format के safeParse के मामले में भी, यह zod API से प्रभावित एक फीचर है.

रुचि दिखाने के लिए धन्यवाद!