A module that parses a string that may be a URL or a social media handle. It returns a url, platform key, and an identifier, plus a valid key that is true or false.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
input | string | The input string that may be a URL or a social media handle. Defaults to an empty string. | |
platformHint | string | <optional> | Optional. The key of the platform (e.g., 'xitter', 'facebook') to help identify the input if it's a handle. |
- Since
- 4.0.6
- Source
Returns:
An object with the following properties, url, identifier, platform, and valid.
- Type:
- object
Example
import { parseSocial } from '@gravityforms/utils';
// Example 1: Parsing a URL
const twitterProfile = parseSocial('https://twitter.com/elonmusk');
// twitterProfile: { url: 'https://x.com/elonmusk', identifier: 'elonmusk', platform: 'xitter', valid: true }
// Example 2: Parsing a handle with a hint
const facebookHandle = parseSocial('zuck', 'facebook');
// facebookHandle: { url: 'https://facebook.com/zuck', identifier: 'zuck', platform: 'facebook', valid: true }
// Example 3: Parsing an ambiguous handle without a hint (likely invalid)
const ambiguousHandle = parseSocial('username123');
// ambiguousHandle: { url: '', identifier: '', platform: '', valid: false }
// Example 4: Invalid input
const invalidInput = parseSocial('not a url or handle', 'randomPlatform');
// invalidInput: { url: '', identifier: '', platform: '', valid: false }