### NPM Installation Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Install the Sanscript library using npm. ```bash npm install @indic-transliteration/sanscript ``` -------------------------------- ### Sanscript.detect() Examples Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Examples demonstrating the usage of Sanscript.detect() with different inputs to identify transliteration schemes. ```javascript Sanscript.detect('namaste'); // "HK" (no SLP1 marks) ``` ```javascript Sanscript.detect('namas=te'); // "HK" (common chars) ``` ```javascript Sanscript.detect('Nama=te'); // Could match SLP1 if P, B, etc. present ``` ```javascript Sanscript.detect('nama.ste'); // "Velthuis" (contains .) ``` ```javascript Sanscript.detect('nama"ste'); // "Velthuis" (contains ") ``` ```javascript Sanscript.detect('namas~te'); // "Velthuis" (contains ~) ``` ```javascript Sanscript.detect('naama'); // "ITRANS" (shared aa, no other marks) ``` ```javascript Sanscript.detect('na~na'); // "ITRANS" (shared ~n) ``` ```javascript Sanscript.detect('namaste'); // "HK" (no special diacritics) ``` ```javascript Sanscript.detect('rama'); // "HK" (generic ASCII) ``` ```javascript Sanscript.detect('123'); // "HK" (no patterns match) ``` -------------------------------- ### Performance Caching Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Demonstrates cache hits and misses for transliteration, showing performance differences. ```javascript Sanscript.t('नमस्ते', 'devanagari', 'iast'); // Cache miss, slower Sanscript.t('धन्यवाद', 'devanagari', 'iast'); // Cache hit, faster ``` -------------------------------- ### Ambiguous Input Detection Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Demonstrates how Sanscript.detect() handles ambiguous inputs, prioritizing patterns based on a defined order. ```javascript Sanscript.detect('devanagari ENGLISH'); // "Devanagari" (stops at first 'd' in devanagari before seeing ENGLISH) ``` -------------------------------- ### Complete Roman Scheme Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md This example demonstrates a complete Roman script scheme definition. It includes the `isRomanScheme` flag set to true, along with mappings for vowels, consonants, and symbols. Optional fields like `vowel_marks` can be auto-generated if omitted. ```javascript { isRomanScheme: true, vowels: { 'a': 'a', 'ā': 'aa', 'i': 'i', 'ī': 'ii', 'u': 'u', 'ū': 'uu', 'ṛ': 'ri', 'ṝ': 'rii', 'ḷ': 'li', 'ḹ': 'lii', 'e': 'e', 'ai': 'ai', 'o': 'o', 'au': 'au' }, consonants: { 'k': 'ka', 'kh': 'kha', 'g': 'ga', 'gh': 'gha', 'ṅ': 'nga', 'c': 'cha', 'ch': 'chha', 'j': 'ja', 'jh': 'jha', 'ñ': 'nya', // ... 26+ more }, symbols: { '0': '0', '1': '1', // ... 14+ symbols } // vowel_marks auto-generated if omitted } ``` -------------------------------- ### Example PreferredAlternates Object Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Shows an example of the `PreferredAlternates` object, defining custom replacements for 'iast' and 'itrans' schemes. This is useful for fine-tuning transliteration output. ```javascript const alternates = { iast: { 'ā': 'aa', // Replace long 'a' with double 'a' 'ī': 'ii', // Replace long 'i' with double 'i' 'ū': 'uu', // Replace long 'u' with double 'u' 'ṅ': 'ng', // Replace retroflex nasal 'j~n': 'GY' // Replace retroflex j }, itrans: { 'N': 'NR', // Different alternates for ITRANS 'R': 'RH' } } ``` -------------------------------- ### Include Sanscript in Browser (AMD/requirejs) Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md This example shows how to include Sanscript using AMD loaders like requirejs in a browser environment. ```html ``` -------------------------------- ### Sanscript.t() Usage Examples Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Examples showing the correct and potentially incorrect usage of Sanscript.t() for transliteration, highlighting the importance of explicit 'from' parameter. ```javascript // May be wrong due to ambiguity Sanscript.t('namaste', '', 'devanagari'); // Explicit and safe Sanscript.t('namaste', 'hk', 'devanagari'); ``` ```javascript // Auto-detect (may be wrong) Sanscript.t('namaste', '', 'devanagari'); // Explicit (always correct) Sanscript.t('namaste', 'hk', 'devanagari'); Sanscript.t('namaste', 'iast', 'devanagari'); Sanscript.t('नमस्ते', 'devanagari', 'hk'); ``` -------------------------------- ### Minimal Brahmic Scheme Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md This is a complete example of a minimal Brahmic script scheme definition. It includes mappings for vowels, virama, consonants, and symbols required for transliteration. ```javascript { vowels: { 'अ': 'a', 'आ': 'aa', 'इ': 'i', 'ई': 'ii', 'उ': 'u', 'ऊ': 'uu', 'ऋ': 'ri', 'ॠ': 'rii', 'ऌ': 'li', 'ॡ': 'lii', 'ए': 'e', 'ऐ': 'ai', 'ओ': 'o', 'औ': 'au' }, virama: { '्': '' }, consonants: { 'क': 'ka', 'ख': 'kha', 'ग': 'ga', 'घ': 'gha', 'ङ': 'nga', 'च': 'cha', 'छ': 'chha', 'ज': 'ja', 'झ': 'jha', 'ञ': 'nya', 'ट': 'ta', 'ठ': 'tha', 'ड': 'da', 'ढ': 'dha', 'ण': 'na', 'त': 'ta', 'थ': 'tha', 'द': 'da', 'ध': 'dha', 'न': 'na', 'प': 'pa', 'फ': 'pha', 'ब': 'ba', 'भ': 'bha', 'म': 'ma', 'य': 'ya', 'र': 'ra', 'ल': 'la', 'व': 'va', 'श': 'sha', 'ष': 'sha', 'स': 'sa', 'ह': 'ha', 'ळ': 'lla' }, symbols: { 'ॐ': 'om', '।': '.', '॥': '..', '०': '0', '१': '1', '२': '2', '३': '3', '४': '4', '५': '5', '६': '6', '७': '7', '८': '8', '९': '9' } } ``` -------------------------------- ### Cache Behavior Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Demonstrates Sanscript's runtime caching. The first call with specific options results in a cache miss and map build, while subsequent calls with identical options result in a cache hit. ```javascript // First call: cache miss, builds map Sanscript.t('नमस्ते', 'devanagari', 'iast'); // Second call: cache hit, reuses map (faster) Sanscript.t('धन्यवाद', 'devanagari', 'iast'); // Cache miss: different options Sanscript.t('नमस्ते', 'devanagari', 'iast', { syncope: true }); ``` -------------------------------- ### Including Sanscript via Script Tag Source: https://github.com/indic-transliteration/sanscript.js/blob/master/README.md Provides an example of how to include the Sanscript library in an HTML document using a script tag, making the Sanscript object globally available. ```html ``` -------------------------------- ### Efficient Cache Reuse in Sanscript.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This example shows an efficient method for using Sanscript.t by reusing a single options object, leading to a single cache hit and improved performance when transliterating multiple words. ```javascript const options = { syncope: true }; for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast', options); console.log(result); } ``` -------------------------------- ### Example Minimal Brahmic Scheme Object Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Illustrates a basic Scheme object for a Brahmic script like Devanagari. It includes essential groups like vowels, virama, consonants, and symbols. ```javascript { vowels: { 'अ': 'a', 'आ': 'aa', 'इ': 'i', 'ई': 'ii', 'उ': 'u', 'ऊ': 'uu', 'ऋ': 'ri', 'ॠ': 'ri', 'ऌ': 'li', 'ॡ': 'li', 'ए': 'e', 'ऐ': 'ai', 'ओ': 'o', 'औ': 'au' // ... 14+ total }, virama: { '्': '' // or Devanagari virama to target virama }, consonants: { 'क': 'ka', 'ख': 'kha', 'ग': 'ga', 'घ': 'gha', // ... 36+ consonants and consonant clusters }, symbols: { '०': '0', '१': '1', '२': '2', '।': '.', '॥': '...' // ... 14+ symbols }, other: { 'य': 'ya', 'र': 'ra', 'ल': 'la' }, vowel_marks: { 'ा': 'aa', 'ि': 'i', 'ी': 'ii', // ... 15+ vowel marks (auto-generated if omitted) } } ``` -------------------------------- ### Handle Edge Cases in Detection Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Examples demonstrating how Sanscript.detect() handles edge cases such as empty strings, numbers only, and mixed content. ```javascript Sanscript.detect(''); // "HK" (empty, no matches) Sanscript.detect('123'); // "HK" (numbers only) Sanscript.detect('नमस्ते 123 text'); // "Devanagari" (first char is Brahmic) Sanscript.detect('text नमस्ते'); // "HK" (first char is ASCII) ``` -------------------------------- ### Vowels Group Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md Defines the mapping for Devanagari vowels to the scheme's vowels. A minimum of 14 entries is required. ```javascript vowels: { 'अ': 'a', 'आ': 'aa', 'इ': 'i', 'ई': 'ii', 'उ': 'u', 'ऊ': 'uu', 'ऋ': 'ri', 'ॠ': 'rii', 'ऌ': 'li', 'ॡ': 'lii', 'ए': 'e', 'ऐ': 'ai', 'ओ': 'o', 'औ': 'au' // 14 required; may have 16 with short e/o } ``` -------------------------------- ### Example Roman Scheme Object Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Demonstrates a Scheme object configured for a Roman script. It includes the `isRomanScheme` flag and maps Roman characters to their transliterated forms. ```javascript { isRomanScheme: true, vowels: { 'a': 'a', 'ā': 'aa', 'i': 'i', 'ī': 'ii', // ... 16+ vowels }, consonants: { 'ka': 'ka', 'kha': 'kha', 'ga': 'ga', // ... 36+ consonants without inherent vowel }, symbols: { '0': '0', '1': '1', // ... 14+ symbols } // vowel_marks auto-generated if omitted } ``` -------------------------------- ### Sanscript Options with Preferred Alternates Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Shows how to configure preferred character alternates within the Options object for custom transliteration mappings. This example maps specific Sanskrit characters to their common Romanized equivalents. ```javascript const options = { preferred_alternates: { iast: { 'ā': 'aa', 'ī': 'ii', 'ū': 'uu' } } }; const result = Sanscript.t('ramah', 'hk', 'iast', options); // If output would contain 'ā', it will be replaced with 'aa' ``` -------------------------------- ### Per-Call Options Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Override default Sanscript options for a single transliteration call by passing an `options` object as the fourth argument to `Sanscript.t`. This allows for temporary configuration changes without affecting global defaults. ```javascript // Use syncope only for this call const result = Sanscript.t('ajay', 'itrans', 'devanagari', { syncope: true }); // Leave defaults unchanged console.log(Sanscript.defaults.syncope); // false ``` -------------------------------- ### Inefficient Cache Misses in Sanscript.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This example demonstrates an inefficient way to use Sanscript.t, where cache misses occur on each iteration due to creating new option objects. Avoid this pattern when processing multiple words with the same options. ```javascript const words = ['नमस्ते', 'धन्यवाद', 'शुभं']; for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast', { syncope: true }); console.log(result); } ``` -------------------------------- ### Consonants Group Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md Maps Devanagari consonants to scheme consonants. For Roman schemes, consonants do not have an inherent vowel and are mapped as bare consonants. ```javascript consonants: { 'क': 'ka', 'ख': 'kha', 'ग': 'ga', 'घ': 'gha', 'ङ': 'nga', 'च': 'cha', 'छ': 'chha', 'ज': 'ja', 'झ': 'jha', 'ञ': 'nya', // ... etc (36+ total) } ``` ```javascript // IAST consonants: { 'k': 'ka', // Roman 'k' maps to IAST 'ka' 'kh': 'kha', // ... } ``` -------------------------------- ### Syncope Example: Default vs. Hindi-style Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Compares the output of transliterating 'bhagavat' with and without the `syncope` option enabled. Demonstrates how `syncope: true` removes the virama from the final consonant, mimicking Hindi pronunciation. ```javascript // Default (lossless) Sanscript.t('bhagavat', 'itrans', 'devanagari'); // Output: "भगवत्" (virama after 't') ``` ```javascript // With syncope (lossy, Hindi-style) Sanscript.t('bhagavat', 'itrans', 'devanagari', { syncope: true }); // Output: "भगवत" (no virama) ``` -------------------------------- ### Brahmic Script Detection Example Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Demonstrates how Sanscript detects Brahmic scripts by checking character code points against Unicode block ranges. The process involves matching the character's code point against sorted Brahmic script ranges to identify the script. ```javascript const char = 'क'; // U+0915 const code = char.charCodeAt(0); // 2325 (decimal) // 0x0900 (2304) <= 0x0915 (2325) <= 0x0D7F (3455) // Falls in Brahmic range // Match against blocks (descending by code point): // 0x0D00 (3328) > 0x0915? No // 0x0C80 (3200) > 0x0915? No // 0x0C00 (3072) > 0x0915? No // 0x0B80 (2944) > 0x0915? No // 0x0B00 (2816) > 0x0915? No // 0x0A80 (2688) > 0x0915? No // 0x0A00 (2560) > 0x0915? No // 0x0900 (2304) >= 0x0915? No, found Devanagari Result: "Devanagari" ``` -------------------------------- ### Replace Macron with Double Vowel using preferred_alternates Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Use `preferred_alternates` to specify character replacements after transliteration. This example replaces macrons with double vowels for a specific scheme. ```javascript const result = Sanscript.t('ram', 'hk', 'iast', { preferred_alternates: { iast: { 'ā': 'aa', 'ī': 'ii', 'ū': 'uu' } } }); // If output would be 'rāma', becomes 'raama' ``` -------------------------------- ### Creating Interlinear Text for Language Learning with Sanscript.transliterateWordwise Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This function generates parallel text for language learning by using Sanscript.transliterateWordwise to create pairs of original and transliterated words. It's suitable for building language education software or pronunciation guides. ```javascript function createInterlinear(text, fromScript, toScript) { const pairs = Sanscript.transliterateWordwise(text, fromScript, toScript); let output = ''; for (const [original, transliterated] of pairs) { output += `${original}\n${transliterated}\n\n`; } return output; } // Usage const text = 'नमस्ते भवान्'; console.log(createInterlinear(text, 'devanagari', 'iast')); // Output: // नमस्ते // namaste // // भवान् // bhavān // ``` -------------------------------- ### Basic Sanscript Options Usage Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Demonstrates how to set up and use the basic Options object with Sanscript.t() for transliteration. Ensure the 'itrans' and 'devanagari' schemes are available. ```javascript const options = { skip_sgml: true, syncope: false, preferred_alternates: {} }; const result = Sanscript.t('text', 'itrans', 'devanagari', options); ``` -------------------------------- ### Virama Group Examples Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md Maps the virama character, which suppresses the inherent vowel of consonants. Examples show mapping for Devanagari, IAST (Roman), and a custom scheme. ```javascript // In Devanagari virama: { '्': '्' // Virama to virama (identity) } // In IAST (Roman) virama: { '्': '' // Devanagari virama to nothing (no explicit mark) } // In custom scheme virama: { '्': '~' // Devanagari virama to tilde } ``` -------------------------------- ### Run QUnit Tests Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Execute `npm run test` to run the QUnit test suite for Sanscript. ```bash npm run test ``` -------------------------------- ### Publish Sanscript.js to npm Source: https://github.com/indic-transliteration/sanscript.js/blob/master/README.md Run this command in the project root to publish the package to the npm registry. Ensure the package is set to public access. ```shell npm publish --access public ``` -------------------------------- ### Most Efficient Cache Usage with Pre-set Defaults in Sanscript.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This demonstrates the most efficient way to leverage Sanscript.js caching by setting global defaults, eliminating the need to pass options on each call. ```javascript Sanscript.defaults.syncope = true; for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast'); console.log(result); } ``` -------------------------------- ### Basic Usage of Sanscript.t Source: https://github.com/indic-transliteration/sanscript.js/blob/master/README.md Demonstrates the fundamental usage of the Sanscript.t function for transliteration. Requires input string, source scheme, and target scheme. ```javascript var output = Sanscript.t(input, from, to); ``` -------------------------------- ### Build ES5 Output Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Run `npm run dist` to build the ES5 JavaScript output file (`sanscript.js`) for browser compatibility. ```bash npm run dist ``` -------------------------------- ### Load Sanscript using AMD (RequireJS) Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Load the Sanscript library in an AMD environment using RequireJS's `require` function. ```javascript require(['sanscript'], function(Sanscript) { var result = Sanscript.t(input, 'devanagari', 'iast'); }); ``` -------------------------------- ### Using Sanscript with Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/README.md Demonstrates calling the Sanscript.t function with an additional 'options' object to customize transliteration behavior. The 'options' object can modify aspects like SGML tag handling or syncope. ```javascript var output = Sanscript.t(input, from, to, options); ``` -------------------------------- ### Lint Code with ESLint Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Execute `npm run lint` to perform code linting using ESLint. ```bash npm run lint ``` -------------------------------- ### Include Sanscript in Browser (Global) Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md This demonstrates how to use Sanscript when included via a global script tag in a browser. ```html ``` -------------------------------- ### ES Modules Usage Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Import Sanscript using ES Modules syntax. ```javascript import Sanscript from '@indic-transliteration/sanscript'; ``` -------------------------------- ### Applying Preferred Alternates in Sanscript.t Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Demonstrates how to apply custom character replacements using the `preferred_alternates` option within the `Sanscript.t` function. This allows for post-transliteration modifications. ```javascript const output = Sanscript.t('rama', 'hk', 'iast', { preferred_alternates: { iast: { 'ā': 'aa' } } }); // If 'rāma' would be output, becomes 'raama' ``` -------------------------------- ### CommonJS Usage Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Import Sanscript using CommonJS module syntax. ```javascript const Sanscript = require('@indic-transliteration/sanscript'); ``` -------------------------------- ### Minimizing Options Overhead with Defaults Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Changing options frequently invalidates the cache. Set default options once using `Sanscript.defaults` for best performance, allowing subsequent calls to reuse the cache. ```javascript // Set defaults once Sanscript.defaults.syncope = true; // Subsequent calls don't rebuild maps for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast'); } ``` -------------------------------- ### Efficient Caching with Reused Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md For high-throughput transliteration, reuse the same 'from', 'to', and 'options' combination to benefit from caching. This avoids cache misses on each iteration. ```javascript // Inefficient: cache miss on each iteration for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast', { syncope: true }); } // Efficient: single cache hit, reused for all iterations const options = { syncope: true }; for (const word of words) { const result = Sanscript.t(word, 'devanagari', 'iast', options); } ``` -------------------------------- ### Internal Replacement Logic for preferred_alternates Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Illustrates the internal batch replacement mechanism used by `preferred_alternates`, which employs split-join operations. ```javascript // Internally, for each key in preferred_alternates[to]: result = result.split(key).join(replacement); ``` -------------------------------- ### Use a Registered Custom Scheme Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md After registering a custom scheme, it can be used immediately with the `Sanscript.t` function. ```javascript const output = Sanscript.t('text', 'devanagari', 'my_roman'); ``` -------------------------------- ### Browser Application for Transliteration Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Implement a frontend application that allows users to transliterate text directly in the browser. This involves HTML for the user interface and JavaScript to handle user input and display output. ```html // HTML
``` ```javascript // JavaScript ``` -------------------------------- ### Configuring Preferred Alternates Source: https://github.com/indic-transliteration/sanscript.js/blob/master/README.md Shows how to use the 'preferred_alternates' option to specify custom mappings for transliteration. This is useful for controlling specific character representations, like choosing 'aa' over 'A' for itrans. ```javascript { itrans : { "A" : "aa", "I" : "ii", "U" : "uu", "j~n" : "GY" } } ``` -------------------------------- ### Load Sanscript Globally in Browser Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Include the `sanscript.js` file via a ` ``` -------------------------------- ### Combined Transliteration Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Demonstrates using multiple advanced options, such as syncope and preferred alternates, simultaneously for complex output requirements. ```javascript const result = Sanscript.t('IAST_input', 'iast', 'devanagari', { syncope: true, preferred_alternates: { devanagari: { 'ं': 'M', // Some targets may want this 'ः': 'H' } } }); ``` -------------------------------- ### Saving and Restoring Defaults in Sanscript.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This pattern shows how to save current Sanscript.js defaults, modify them for a specific operation, and then restore them. This is crucial for maintaining predictable behavior across different parts of an application. ```javascript // Save current defaults const originalDefaults = { skip_sgml: Sanscript.defaults.skip_sgml, syncope: Sanscript.defaults.syncope, preferred_alternates: { ...Sanscript.defaults.preferred_alternates } }; // Change for specific operation Sanscript.defaults.syncope = true; const result1 = Sanscript.t(text, 'itrans', 'devanagari'); // Restore Sanscript.defaults.skip_sgml = originalDefaults.skip_sgml; Sanscript.defaults.syncope = originalDefaults.syncope; Sanscript.defaults.preferred_alternates = originalDefaults.preferred_alternates; const result2 = Sanscript.t(text, 'itrans', 'devanagari'); ``` -------------------------------- ### Validate Schemes Against JSON Schema Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Run `npm run validate` to validate the transliteration schemes against the defined JSON schema. ```bash npm run validate ``` -------------------------------- ### TypeScript Type Definitions and Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Utilize TypeScript for full type support and configure options like 'syncope' and 'preferred_alternates'. ```typescript import Sanscript, { Options, Scheme, PreferredAlternates } from '@indic-transliteration/sanscript'; const options: Options = { syncope: true, preferred_alternates: { iast: { 'ā': 'aa' } } }; const result = Sanscript.t('rama', 'hk', 'iast', options); ``` -------------------------------- ### Access Default Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md You can access the global default options object using `Sanscript.defaults`. This object holds configuration that applies to all transliteration calls unless explicitly overridden. ```javascript Sanscript.defaults ``` -------------------------------- ### Browser Usage Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Include Sanscript in the browser using a script tag and access it globally. ```html ``` -------------------------------- ### Importing Sanscript Types with TypeScript Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Import all available types, including Options, Scheme, and PreferredAlternates, directly from the Sanscript package. ```typescript import Sanscript, { Options, Scheme, PreferredAlternates } from '@indic-transliteration/sanscript'; ``` -------------------------------- ### Using Sanscript Namespace with TypeScript Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/types.md Import the Sanscript namespace to access its types, such as Options, and use them for configuring Sanscript functionalities. ```typescript import Sanscript from '@indic-transliteration/sanscript'; const opts: Sanscript.Options = { syncope: true }; ``` -------------------------------- ### Default Options Property Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/INDEX.md Provides access to the default transliteration options. ```APIDOC ## Sanscript.defaults ### Description An object containing the default options used for transliteration. These can be accessed and modified. ### Property `Sanscript.defaults` ### Type Object ### Example ```javascript console.log(Sanscript.defaults.syncope); Sanscript.defaults.skip_sgml = true; ``` ``` -------------------------------- ### Web API Integration with Express.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Set up a REST API endpoint using Express.js to handle transliteration requests. This is suitable for building microservices or exposing transliteration functionality to web clients. ```javascript // Express.js example const express = require('express'); const Sanscript = require('@indic-transliteration/sanscript'); const app = express(); app.post('/transliterate', express.json(), (req, res) => { const { text, from, to } = req.body; try { const result = Sanscript.t(text, from || undefined, to); res.json({ success: true, result }); } catch (error) { res.status(400).json({ success: false, error: error.message }); } }); app.listen(3000, () => { console.log('Transliteration service running on port 3000'); }); ``` -------------------------------- ### Explicit Source Specification for Transliteration Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Always specifies the source script for reliable and performant transliteration, recommended for production systems. Avoids detection overhead. ```javascript // Always explicit (recommended for production) const result = Sanscript.t('नमस्ते', 'devanagari', 'iast'); // Not recommended (relies on detection) const result2 = Sanscript.t('नमस्ते', '', 'iast'); ``` -------------------------------- ### Registering Custom Brahmic Script Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/INDEX.md Allows users to register a new Brahmic script scheme with Sanscript. ```APIDOC ## Sanscript.addBrahmicScheme(name, scheme) ### Description Registers a custom Brahmic script `scheme` under the given `name`. ### Method `Sanscript.addBrahmicScheme` ### Parameters - **name** (string) - The unique name for the new Brahmic script. - **scheme** (object) - The definition of the Brahmic script scheme. ### Request Example ```javascript const myScheme = { a: 'अ', i: 'इ' }; Sanscript.addBrahmicScheme('mybrahmic', myScheme); ``` ### Response None. ``` -------------------------------- ### Universal Script Conversion with Sanscript.t and Sanscript.detect Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md This function creates a universal converter that auto-detects the source script and converts text to a target scheme. It's ideal for language learning applications or transliteration utilities requiring any-to-any conversion. ```javascript function convertScript(text, targetScheme) { // Auto-detect source const sourceScheme = Sanscript.detect(text); if (sourceScheme === targetScheme) { return text; // No conversion needed } // Convert return Sanscript.t(text, sourceScheme.toLowerCase(), targetScheme); } // Usage const devanagari = 'नमस्ते'; const iast = convertScript(devanagari, 'iast'); // "namaste" const bengali = convertScript(devanagari, 'bengali'); // "নমস্তে" ``` -------------------------------- ### addBrahmicScheme(name, scheme) Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md Adds or registers a new Brahmic (abugida) script scheme to be used with Sanscript. This allows for custom script definitions and transliterations. ```APIDOC ## addBrahmicScheme(name, scheme) ### Description Adds or registers a new Brahmic (abugida) script scheme to be used with Sanscript. This allows for custom script definitions and transliterations. ### Method `Sanscript.addBrahmicScheme` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **name** (string) - Required - Unique scheme identifier (e.g., 'devanagari', 'bengali') - **scheme** (Scheme) - Required - Scheme definition object with character groups ### Returns `void` - Modifies `Sanscript.schemes` in-place ### Scheme Structure A Brahmic scheme is an object mapping group names to character arrays. Required groups: - `vowels` — Independent vowel forms (16+ items) - `virama` — Diacritic to suppress inherent vowel (1 item) - `consonants` — Consonants with inherent 'a' vowel (36+ items) - `symbols` — Numbers and punctuation (14+ items) Optional groups: - `vowel_marks` — Dependent vowel forms (15+ items) - `yogavaahas` — Special marks (anusvara, visarga, etc.) - `other` — Miscellaneous consonants - `accents` — Vedic accent marks - `accented_vowel_alternates` — Alternates for accented vowels - `zwj` — Zero-width joiner - `alternates` — Character alternates for parsing - `shortcuts` — Shorthand mappings - `candra` — Candra mark ### Examples ```javascript Sanscript.addBrahmicScheme('custom_script', { vowels: { 'अ': 'A', 'आ': 'Aa', ... }, virama: { '्': '' }, consonants: { 'क': 'Ka', 'ख': 'Kha', ... }, symbols: { '०': '0', '१': '1', ... } }); const output = Sanscript.t('काँ', 'custom_script', 'iast'); ``` ``` -------------------------------- ### Loaded Schemes Property Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/INDEX.md Provides access to all loaded script and Romanization schemes. ```APIDOC ## Sanscript.schemes ### Description An object containing all currently loaded script and Romanization schemes. ### Property `Sanscript.schemes` ### Type Object ### Example ```javascript console.log(Sanscript.schemes.itrans); ``` ``` -------------------------------- ### Register a New Brahmic Scheme Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Use `Sanscript.addBrahmicScheme` to register a custom Brahmic transliteration scheme with its character mappings. ```javascript Sanscript.addBrahmicScheme('my_script', { vowels: { /* ... */ }, virama: { /* ... */ }, consonants: { /* ... */ }, symbols: { /* ... */ } // ... other optional groups }); ``` -------------------------------- ### Interlinear Display with Wordwise Transliteration Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/README.md Generate word-by-word transliteration pairs for interlinear display. ```javascript const pairs = Sanscript.transliterateWordwise('नमस्ते', 'devanagari', 'iast'); pairs.forEach(([orig, trans]) => { console.log(orig); console.log(trans); }); ``` -------------------------------- ### Registering Custom Roman Scheme Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/INDEX.md Allows users to register a new Romanization scheme with Sanscript. ```APIDOC ## Sanscript.addRomanScheme(name, scheme) ### Description Registers a custom Romanization `scheme` under the given `name`. ### Method `Sanscript.addRomanScheme` ### Parameters - **name** (string) - The unique name for the new Roman scheme. - **scheme** (object) - The definition of the Romanization scheme. ### Request Example ```javascript const myRomanScheme = { \u0905: 'a', \u0906: 'aa' }; Sanscript.addRomanScheme('myroman', myRomanScheme); ``` ### Response None. ``` -------------------------------- ### Validate Schemes During Build Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md The `npm run validate` command checks the validity of scheme files against the JSON schema. ```bash npm run validate # Validates src/schemes/*/*.json files ``` -------------------------------- ### Per-Call Options Merge with Defaults Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Options provided in a per-call `options` object merge with the global `Sanscript.defaults`. Any options not specified in the per-call object will inherit their values from the defaults. ```javascript Sanscript.defaults.skip_sgml = true; // This call inherits skip_sgml: true from defaults const result = Sanscript.t(text, 'itrans', 'devanagari', { syncope: true // skip_sgml implicitly true from defaults }); ``` -------------------------------- ### Round-Trip Testing for Lossless Transliteration Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Implement a round-trip test function to verify that transliterating text from one script to another and back preserves the original text. This is crucial for ensuring script pair compatibility and preventing information loss. ```javascript function testRoundTrip(originalText, script1, script2) { // Transliterate forward const step1 = Sanscript.t(originalText, script1, script2); // Transliterate back const step2 = Sanscript.t(step1, script2, script1); // Check if round-trip preserves original return step2 === originalText; } // Test const isLossless = testRoundTrip('नमस्ते', 'devanagari', 'iast'); console.log(isLossless); // Should be true for lossless pairs ``` -------------------------------- ### Access Loaded Schemes Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md Loaded schemes are available via `Sanscript.schemes`. You can access all schemes or a specific one by its name, such as 'devanagari' or 'iast'. ```javascript // All loaded schemes Sanscript.schemes // Specific scheme Sanscript.schemes['devanagari'] Sanscript.schemes['iast'] ``` -------------------------------- ### Sanscript.t(data, from, to, options) Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md The main function for transliterating text from one script to another. It can automatically detect the source script if not provided and supports various options for controlling the transliteration behavior. ```APIDOC ## Sanscript.t(data, from, to, options) ### Description Converts text from one script to another. The source script can be auto-detected if not provided. ### Method `Sanscript.t(data: string, from?: string, to: string, options?: Options): string` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **data** (string) - Required - The text to transliterate. - **from** (string) - Optional - Source script/scheme name (e.g., 'devanagari', 'iast', 'hk'). Defaults to auto-detected script. - **to** (string) - Required - Destination script/scheme name. - **options** (Options) - Optional - Transliteration behavior options. ### Request Example ```javascript const output = Sanscript.t('नमस्ते', 'devanagari', 'iast'); // output: "namaste" ``` ### Response #### Success Response - **string**: Transliterated text in the destination script. #### Response Example ```javascript "namaste" ``` ``` -------------------------------- ### Detect Brahmic Scripts Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/script-detection.md Use Sanscript.detect() to identify various Brahmic scripts based on their characters. ```javascript Sanscript.detect('नमस्ते'); // "Devanagari" Sanscript.detect('নমস্তে'); // "Bengali" Sanscript.detect('நமஸ்தே'); // "Tamil" Sanscript.detect('నమస్తే'); // "Telugu" Sanscript.detect('ಹರೋ'); // "Kannada" Sanscript.detect('ഹരോ'); // "Malayalam" Sanscript.detect('નમસ્તે'); // "Gujarati" Sanscript.detect('ਹਨ'); // "Gurmukhi" Sanscript.detect('ନମସ୍ତେ'); // "Oriya" ``` -------------------------------- ### Default Transliteration Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md Sets the default options for all Sanscript transliterations. These include flags for skipping SGML tags and enabling syncope, as well as a map for preferred character alternates. ```javascript Sanscript.defaults = { skip_sgml: false, syncope: false, preferred_alternates: {} } ``` -------------------------------- ### Setting Global Defaults in Sanscript.js Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Configure Sanscript.js behavior for all subsequent transliterations by setting global defaults. This is useful for processing large batches with consistent behavior or setting application-wide preferences. ```javascript // Before processing batch Sanscript.defaults.skip_sgml = true; Sanscript.defaults.syncope = true; Sanscript.defaults.preferred_alternates = { iast: { 'ā': 'aa', 'ī': 'ii' } }; // All subsequent calls inherit these defaults for (const text of batch) { const result = Sanscript.t(text, 'itrans', 'devanagari'); // All use the configured defaults } ``` -------------------------------- ### Register Custom Brahmic Script Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/usage-patterns.md Add support for custom or extended Brahmic script schemes. Useful for legacy encodings or dialect-specific scripts. ```javascript Sanscript.addBrahmicScheme('custom_brahmic', { vowels: { 'अ': 'a', 'आ': 'aa', 'इ': 'i', 'ई': 'ii', 'उ': 'u', 'ऊ': 'uu', 'ऋ': 'ri', 'ॠ': 'rii', 'ऌ': 'li', 'ॡ': 'lii', 'ए': 'e', 'ऐ': 'ai', 'ओ': 'o', 'औ': 'au' }, virama: { '्': '~' }, consonants: { 'क': 'K', 'ख': 'KH', 'ग': 'G', 'घ': 'GH', // ... all 36+ consonants }, symbols: { 'ॐ': 'om', '।': '.', '॥': '..', '०': '0', '१': '1', '२': '2', '३': '3', '४': '4', '५': '5', '६': '6', '७': '7', '८': '8', '९': '9' } }); // Now available for use const result = Sanscript.t('नमस्ते', 'devanagari', 'custom_brahmic'); // Result: "NaMaSTe~" ``` -------------------------------- ### Accessing Transliteration Schemes Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md Retrieves the scheme object for a given transliteration scheme name. This allows inspection or modification of scheme definitions. ```javascript Sanscript.schemes[schemeName] // returns Scheme object ``` -------------------------------- ### Reset Default Options Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Reset the global `Sanscript.defaults` object to its original library values. This is useful to revert any custom default settings. ```javascript Sanscript.defaults = { skip_sgml: false, syncope: false, preferred_alternates: {} }; ``` -------------------------------- ### Transliteration with Disabled Regions Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/api-reference/sanscript.md Demonstrates transliteration where specific regions are marked to be skipped. This is useful for preserving non-transliterated content within the text. ```javascript const output = Sanscript.t('ga##Na##pa##te', 'hk', 'devanagari'); // output: "गNaपte" ``` -------------------------------- ### Register a New Roman Scheme Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/configuration.md Use `Sanscript.addRomanScheme` to register a custom Roman transliteration scheme, defining its character mappings. ```javascript Sanscript.addRomanScheme('my_roman', { vowels: { /* ... */ }, consonants: { /* ... */ }, symbols: { /* ... */ } // vowel_marks auto-generated if omitted }); ``` -------------------------------- ### Add Custom Brahmic Scheme Source: https://github.com/indic-transliteration/sanscript.js/blob/master/_autodocs/scheme-structure.md Use `Sanscript.addBrahmicScheme` to add a custom Brahmic script scheme. Provide a unique name for the scheme and its corresponding scheme object. ```javascript Sanscript.addBrahmicScheme('my_script', { /* scheme object */ }); ```