diff --git a/__tests__/index.test.jsx b/__tests__/index.test.jsx index 3e9ae8d..51dd14e 100644 --- a/__tests__/index.test.jsx +++ b/__tests__/index.test.jsx @@ -1,7 +1,7 @@ const { fireEvent, render } = require('@testing-library/react'); const React = require('react'); -const { Variable, VARIABLE_REGEXP } = require('../index'); +const { Variable, VARIABLE_REGEXP, MDX_VARIABLE_REGEXP } = require('../index'); describe('single variable', () => { const props = { @@ -238,3 +238,45 @@ describe('VARIABLE_REGEXP', () => { expect('<>').not.toMatch(new RegExp(VARIABLE_REGEXP)); }); }); + +describe('MDX_VARIABLE_REGEXP', () => { + it('should **not** match against periods', () => { + expect('{user.api.key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should **not** match against hyphens', () => { + expect('{user.api-key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should **not** match against spaces', () => { + expect('{user.api key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should **not match against colons', () => { + expect('{user.glossary:term}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should match against underscores', () => { + expect('{user.api_key}').toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should be case insensitive', () => { + expect('{user.apiKeY}').toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should match non-leading numeric characters', () => { + expect('{user.P2P}').toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + expect('{user.123}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + expect('{user.A123}').toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should match non-english characters', () => { + expect('{user.片仮名}').toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); + + it('should NOT match against newlines', () => { + expect('{user.what_the\nfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + expect('{user.what_the\rfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + expect('{user.what_the\r\nfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP)); + }); +}); diff --git a/index.jsx b/index.jsx index ce7ce21..0f25267 100644 --- a/index.jsx +++ b/index.jsx @@ -195,5 +195,16 @@ module.exports.Variable = Variable; // - <> - regular variables // - <> - glossary module.exports.VARIABLE_REGEXP = /(?:\\)?<<((?:(?![\r\n])[-_\p{L}:.\s\d])+)(?:\\)?>>/iu.source; + +/** + * @example `{user.api_key}` + * @example `{user.apiKeY}` + * @example `{user.片仮名}` + * @example `{user.P2P}` + * @see {@link https://stackoverflow.com/a/6926184} + */ +module.exports.MDX_VARIABLE_REGEXP = + /(\\)?\{user.[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*(\\)?\}/iu.source; + module.exports.VariablesContext = VariablesContext; module.exports.SelectedAppContext = SelectedAppContext;