extract-stringtag
v1.0.1
Published
Extracts the internal class name (Tag) from a string formatted as '[object Tag]'.
Readme
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-stringtagUsage
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') // nullLicense
Unlicense
Tests
do a git clone, then run npm test
