@involvex/emoji-cli 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,38 @@
1
+ import charRegex from 'char-regex'
2
+
3
+ export const charRegexMatcher = charRegex()
4
+
5
+ export function asFunction<T extends PropertyKey, Args extends unknown[]>(
6
+ input: ((...args: Args) => T) | T,
7
+ ): (...args: Args) => T {
8
+ return typeof input === 'function' ? input : () => input
9
+ }
10
+
11
+ /**
12
+ * Non spacing mark contained by some emoticons (65039 - '️' - 0xFE0F).
13
+ *
14
+ * It's the 'Variant Form', which provides more information so that emoticons
15
+ * can be rendered as more colorful graphics. FE0E is a unicode text version
16
+ * whereas FE0F should be rendered as a graphical version.
17
+ * The code gracefully degrades.
18
+ */
19
+ const NON_SPACING_MARK = String.fromCharCode(65039)
20
+
21
+ const nonSpacingRegex = new RegExp(NON_SPACING_MARK, 'g')
22
+
23
+ /**
24
+ * Removes the non-spacing-mark from the emoji code.
25
+ *
26
+ * Never send a stripped version to clients, as it kills graphical emoticons.
27
+ */
28
+ export function normalizeCode(code: string) {
29
+ return code.replace(nonSpacingRegex, '')
30
+ }
31
+
32
+ export function normalizeName(name: string) {
33
+ return /:.+:/.test(name) ? name.slice(1, -1) : name
34
+ }
35
+
36
+ export function randomItem<T>(array: T[]) {
37
+ return array[Math.floor(Math.random() * array.length)]
38
+ }
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { which } from './which.js'
4
+
5
+ describe('which', () => {
6
+ it('returns a simple emoji name when given an emoji', () => {
7
+ expect(which('☕')).toBe('coffee')
8
+ })
9
+
10
+ it('returns a simple emoji name as markdown when specified as markdown', () => {
11
+ expect(which('☕', { markdown: true })).toBe(':coffee:')
12
+ })
13
+
14
+ it('returns a skin toned emoji name when given a skin toned emoji', () => {
15
+ expect(which('👍🏾')).toBe('+1')
16
+ })
17
+
18
+ it('returns a skin toned emoji name as markdown when specified as markdown', () => {
19
+ expect(which('👍🏾', { markdown: true })).toBe(':+1:')
20
+ })
21
+
22
+ // see issue #21
23
+ it('should work for flags', () => {
24
+ expect(which('🇲🇽')).toBe('mexico')
25
+ expect(which('🇲🇦')).toBe('morocco')
26
+ })
27
+ })
package/src/which.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { assert } from '@sindresorhus/is'
2
+ import skinTone from 'skin-tone'
3
+
4
+ import { findByCode } from './findByCode.js'
5
+
6
+ export interface WhichOptions {
7
+ markdown?: boolean
8
+ }
9
+
10
+ /**
11
+ * Get an emoji name from an emoji.
12
+ */
13
+ export const which = (
14
+ emoji: string,
15
+ { markdown = false }: WhichOptions = {},
16
+ ) => {
17
+ assert.string(emoji)
18
+ assert.boolean(markdown)
19
+
20
+ const result = findByCode(skinTone(emoji, 'none'))
21
+ if (result === undefined) {
22
+ return undefined
23
+ }
24
+
25
+ return markdown ? `:${result.key}:` : result.key
26
+ }