TOAST UI Code Snippet
TOAST UI Code Snippet (tui-code-snippet) is a comprehensive collection of modular, lightweight utility methods designed to simplify common JavaScript programming tasks. Currently stable at version 2.3.3, the library maintains a steady release cadence with regular updates including bug fixes and new features, such as the `ajax` module introduced in v2.3.0. A key differentiator since its 2.0 major release is its transition to a fully modular architecture, requiring developers to import individual functions or modules (e.g., `tui-code-snippet/array/inArray`) rather than a monolithic bundle. This approach minimizes bundle sizes by ensuring only the necessary utilities are included in an application. It provides functionalities across various domains including array manipulation, browser detection, collection processing, custom event management, DOM manipulation, class definition, date formatting, inheritance patterns, object utilities, and type checking. It explicitly supports both ES6 module syntax and CommonJS `require` patterns, making it versatile for modern JavaScript development environments. The library is a foundational component within the broader NHN TOAST UI ecosystem.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'inArray') OR TypeError: Cannot read property 'inArray' of undefined
cause Attempting to access utility functions from a top-level 'tui' or 'util' object after importing the main package, which is no longer supported since v2.0. You are likely importing `tui-code-snippet` monolithically instead of individual functions.fixEnsure you are importing individual functions from their specific file paths (e.g., `import inArray from 'tui-code-snippet/array/inArray';`). Do not try to access `tui.array.inArray` or similar patterns. -
Module not found: Error: Can't resolve 'tui-code-snippet'
cause This error can occur if you're trying to import the root package directly as a main entry point for specific functions without a bundler correctly resolving it, or if you're trying to access a sub-path that doesn't exist. It can also happen if `tui-code-snippet` is not correctly installed via npm or if you migrated from older versions (pre-v2.0) and retained CDN/Bower references.fixVerify that `tui-code-snippet` is installed in your `node_modules`. If using a bundler (like Webpack/Rollup), ensure your imports point to the exact module files (e.g., `tui-code-snippet/array/inArray`). Ensure you're not trying to import a non-existent bundle file.
Warnings
- breaking Version 2.0 introduced significant breaking changes by moving to an npm-only distribution model. Bower and CDN support were dropped, requiring installation via `npm`.
- breaking Since v2.0, all functions are separated into individual files. You can no longer import a single 'tui-code-snippet' object and access utilities like `tui.array.inArray`. Each function must be imported explicitly from its specific module path (e.g., `import inArray from 'tui-code-snippet/array/inArray';`).
- gotcha From v2.1.0 onwards, the package no longer provides pre-built bundle files. If your project requires a bundled version (e.g., for direct browser inclusion without a module bundler), you must generate it yourself.
Install
-
npm install tui-code-snippet -
yarn add tui-code-snippet -
pnpm add tui-code-snippet
Imports
- inArray
import { inArray } from 'tui-code-snippet';import inArray from 'tui-code-snippet/array/inArray';
- isString
const tui = require('tui-code-snippet'); const isString = tui.type.isString;const isString = require('tui-code-snippet/type/isString'); - ajax
import { ajax } from 'tui-code-snippet';import ajax from 'tui-code-snippet/ajax/index';
Quickstart
import inArray from 'tui-code-snippet/array/inArray';
import isString from 'tui-code-snippet/type/isString';
import decodeHTMLEntity from 'tui-code-snippet/string/decodeHTMLEntity';
import extend from 'tui-code-snippet/object/extend';
console.log('--- TOAST UI Code Snippet Quickstart ---');
// Example 1: Check if an item is in an array
const myArray = [1, 2, 3, 4, 5];
const searchTerm = 3;
console.log(`Is ${searchTerm} in [${myArray}]? ${inArray(searchTerm, myArray)}`);
const notFoundTerm = 6;
console.log(`Is ${notFoundTerm} in [${myArray}]? ${inArray(notFoundTerm, myArray)}`);
// Example 2: Check data type
const myString = "Hello, world!";
const myNumber = 123;
console.log(`"${myString}" is a string? ${isString(myString)}`);
console.log(`${myNumber} is a string? ${isString(myNumber)}`);
// Example 3: Decode HTML entities
const encodedHtml = "<div>Hello & World!</div>";
const decodedHtml = decodeHTMLEntity(encodedHtml);
console.log(`Decoded HTML: ${decodedHtml}`);
// Example 4: Extend an object
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const extendedObj = extend({}, obj1, obj2);
console.log('Extended object:', extendedObj);
console.log('------------------------------------');