Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 1.09 KB

File metadata and controls

39 lines (26 loc) · 1.09 KB

extract-stringtag

A lightweight utility to extract the internal class name (like Function) from a string formatted like [object Function]. This is particularly useful when working with custom objects that implement Symbol.toStringTag or for parsing results from Object.prototype.toString.


Installation

npm install extract-stringtag

Usage

This module identifies if a string follows the standard [object Tag] pattern and returns only the Tag portion. If the input is not a string or doesn't match the pattern, it returns null.

const extractStringTag = require('extract-stringtag');

// Standard built-in tags
extractStringTag('[object Array]')     // 'Array'
extractStringTag('[object Object]')    // 'Object'
extractStringTag('[object Promise]')   // 'Promise'

// Custom tags
extractStringTag('[object MyClass]')   // 'MyClass'

// Invalid inputs
extractStringTag('not a tag')          // null
extractStringTag(123)                  // null
extractStringTag('[object Unclosed')   // null

License

Unlicense

Tests

do a git clone, then run npm test