npm.io
1.0.1 • Published 9 months ago

extract-stringtag

Licence
MIT
Version
1.0.1
Deps
2
Size
3 kB
Vulns
0
Weekly
0
Stars
2

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

Keywords