Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

const usPhoneReString = '^(\\+?1[._ -]?)?(\\(\\d{3}\\)|\\d{3})[._ -]?\\d{3}[._ -]?\\d{4}$'
import { lockdownRE } from './lib/lockdown-re'

export const usPhoneREString = '(\\+?1[._ -]?)?(\\(\\d{3}\\)|\\d{3})[._ -]?\\d{3}[._ -]?\\d{4}'
// Contact info: Matches US phone numbers with optional country code and area code.
export const usPhoneRE = new RegExp(usPhoneReString)
const zipCodeReString = '^\\d{5}([._ -]?\\d{4})?$'
export const usPhoneRE = lockdownRE(usPhoneREString)

export const zipCodeREString = '\\d{5}([._ -]?\\d{4})?'
// Contact info: Matches 5 or 9 digit US zip codes.
export const zipCodeRE = new RegExp(zipCodeReString)
export const zipCodeRE = lockdownRE(zipCodeREString)
75 changes: 50 additions & 25 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import * as pre from './css-color-data'
import { lockdownRe } from './lib/lockdown-re'
import * as pre from './lib/css-color-data'
import { lockdownRE } from './lib/lockdown-re'
import {
floatStr,
zeroTo255Str,
Expand All @@ -24,34 +24,47 @@ import {
zeroTo100FloatPercentStr,
zeroTo255FloatStr,
zeroTo360Str
} from './numbers-strings'
} from './lib/numbers-strings'

const hexColorNonAlphaStr = '#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})'
export const hexColorNoAlphaREString = '#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})'
// Colors/CSS: Matches hex specified RGB colors with no alpha channel.
export const hexColorNoAlphaRE = lockdownRe(hexColorNonAlphaStr)
export const hexColorNoAlphaRE = lockdownRE(hexColorNoAlphaREString)

// level 4 adds alpha hex support
const hexColorAlphaStr = '#([a-fA-F0-9]{6}|[a-fA-F0-9]{8}|[a-fA-F0-9]{3,4})'
export const hexColorAlphaREString = '#([a-fA-F0-9]{6}|[a-fA-F0-9]{8}|[a-fA-F0-9]{3,4})'
// Colors/CSS: Matches hex specified RGBA colors with an alpha channel.
export const hexColorAlphaRE = lockdownRe(hexColorAlphaStr)
export const hexColorAlphaRE = lockdownRE(hexColorAlphaREString)

export const cssPreColors1REString = '(?:' + Object.keys(pre.cssPreColors1).join('|') + ')'
// Colors/CSS: Matches CSS1 predefined color names.
export const cssPreColors1RE = lockdownRe(Object.keys(pre.cssPreColors1))
export const cssPreColors1RE = lockdownRE(cssPreColors1REString)

export const cssPreColors2REString = '(?:' + Object.keys(pre.cssPreColors2).join('|') + ')'
// Colors/CSS: Matches CSS2 predefined color names.
export const cssPreColors2RE = lockdownRe(Object.keys(pre.cssPreColors2))
export const cssPreColors2RE = lockdownRE(cssPreColors2REString)

export const cssPreColors3REString = '(?:' + Object.keys(pre.cssPreColors3).join('|') + ')'
// Colors/CSS: Matches CSS3 predefined color names.
export const cssPreColors3RE = lockdownRe(Object.keys(pre.cssPreColors3))
export const cssPreColors3RE = lockdownRE(cssPreColors3REString)

export const cssPreColorsREString = '(?:' + Object.keys(pre.cssPreColors).join('|') + ')'
// Colors/CSS: Matches CSS4 predefined color names.
export const cssPreColorsRE = lockdownRe(Object.keys(pre.cssPreColors))
export const cssPreColorsRE = lockdownRE(cssPreColorsREString)

const alphaStr = `(${zeroTo1FloatStr}|${zeroTo100PercentStr})`
const rgb1IntStr = `rgb\\((\\s*${zeroTo255Str}\\s*,){2}\\s*${zeroTo255Str}\\s*\\)`
const rgb1PercStr = `rgb\\((\\s*${zeroTo100PercentStr}\\s*,){2}\\s*${zeroTo100PercentStr}\\s*\\)`
const rgba3IntStr = `rgba\\((\\s*${zeroTo255Str}\\s*,){3}\\s*${alphaStr}\\s*\\)`
const rgba3PercStr = `rgba\\((\\s*${zeroTo100PercentStr}\\s*,){3}\\s*${alphaStr}\\s*\\)`

export const rgbFuncREString = '(?:' + [rgb1IntStr, rgb1PercStr].join('|') + ')'
// Colors/CSS: Matches CSS1 'rgb(...) using '0...255 and percent (integer) notation.
export const rgbFuncRE = lockdownRe([rgb1IntStr, rgb1PercStr])
export const rgbFuncRE = lockdownRE(rgbFuncREString)

export const rgbaFuncREString = '(?:' + [rgba3IntStr, rgba3PercStr].join('|') + ')'
// Colors/CSS: Matches CSS3 'rgba(...) using '0...255 and percent (integer) notation.
export const rgbaFuncRE = lockdownRe([rgba3IntStr, rgba3PercStr])
export const rgbaFuncRE = lockdownRE(rgbaFuncREString)

// In level 4, rgba is an alias for rgb, supports floats, and space notation
// NOTE: The spec allows float values like '+.25e2%', which cannot be recognized
// via RE and are not supported.
Expand All @@ -60,24 +73,36 @@ const rgbDecFuncStr = `(\\s*${zeroTo255FloatStr}\\s*,){2}\\s*${zeroTo255FloatStr
const rgbPercFuncStr = `(\\s*${zeroTo100FloatPercentStr}\\s*,){2}\\s*${zeroTo100FloatPercentStr}\\s*(,\\s*${alphaFloatStr}\\s*)?`
const rgbDecSpaceStr = `(\\s*${zeroTo255FloatStr}\\s+){2}${zeroTo255FloatStr}\\s*(/\\s*${alphaFloatStr}\\s*)?`
const rgbPercSpaceStr = `(\\s*${zeroTo100FloatPercentStr}\\s+){2}${zeroTo100FloatPercentStr}\\s*(/\\s*${alphaFloatStr}\\s*)?`
const rgbStr = `rgba?\\((${rgbDecFuncStr}|${rgbPercFuncStr}|${rgbDecSpaceStr}|${rgbPercSpaceStr})\\s*\\)`

export const rgbREString = `rgba?\\((${rgbDecFuncStr}|${rgbPercFuncStr}|${rgbDecSpaceStr}|${rgbPercSpaceStr})\\s*\\)`
// Colors/CSS: Matches CSS4 'rgb(...) and rgba(...) functios using '0...255 and percent (float) notation.
export const rgbRE = lockdownRe(rgbStr)
export const rgbRE = lockdownRE(rgbREString)

const hsl3BaseStr = `\\s*${zeroTo360Str}(deg)?\\s*(,\\s*${zeroTo100PercentStr}\\s*)`
const hsl3Opts = [`hsl\\(${hsl3BaseStr}{2}\\)`, `hsla\\(${hsl3BaseStr}{3}\\)`]

export const hsl3REString = '(?:' + hsl3Opts.join('|') + ')'
// Colors/CSS: Matches CSS3 'hsl(...) and hsla(...) deg and percent notation.
export const hsl3RE = lockdownRe(hsl3Opts)
const hslStr = `hsla?\\(\\s*${floatStr}(deg|grad|rad|turn)?\\s*(,\\s*${zeroTo100FloatPercentStr}\\s*){2,3}\\)`
export const hsl3RE = lockdownRE(hsl3REString)

export const hslREString =
`hsla?\\(\\s*${floatStr}(deg|grad|rad|turn)?\\s*(,\\s*${zeroTo100FloatPercentStr}\\s*){2,3}\\)`
// Colors/CSS: Matches CSS4 'hsl(...) and hsla(...) deg, grad, rad, turn and percent notation.
export const hslRE = lockdownRe(hslStr)
export const hslRE = lockdownRE(hslREString)

const cssColor3Opts = [hexColorNonAlphaStr, rgb1IntStr, rgb1PercStr, rgba3IntStr, rgba3PercStr]
.concat(Object.keys(pre.cssPreColors3))
.concat(hsl3Opts)
export const cssColor3REString = '(?:'
+ [hexColorNoAlphaREString, rgb1IntStr, rgb1PercStr, rgba3IntStr, rgba3PercStr]
.concat(Object.keys(pre.cssPreColors3))
.concat(hsl3Opts)
.join('|')
+ ')'
// Colors/CSS: Matches CSS3 'hex, rgb, rgba, hsl, and predefined colors.
export const cssColor3RE = lockdownRe(cssColor3Opts)
const cssColorOpts = [hexColorAlphaStr, rgbStr, hslStr]
.concat(Object.keys(pre.cssPreColors))
export const cssColor3RE = lockdownRE(cssColor3REString)

const cssColorREString = '(?:'
+ [hexColorAlphaREString, rgbREString, hslREString]
.concat(Object.keys(pre.cssPreColors))
.join('|')
+ ')'
// Colors/CSS: Matches CSS4 'hex, rgb, rgba, hsl, and predefined colors.
export const cssColorRE = lockdownRe(cssColorOpts)
export const cssColorRE = lockdownRE(cssColorREString)
3 changes: 2 additions & 1 deletion src/doc-extractor/doc-extractor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const addData = ({ description, reName, section }) => {
}

(async() => {
// read everything from src root
const files = (await fs.readdir(fsPath.resolve(__dirname, '..'), { withFileTypes : true }))
.filter((f) => f.isFile())
for (const { name: fileName } of files) {
const contents = await fs.readFile(fsPath.resolve(__dirname, '..', fileName), { encoding : 'utf8' })
const lines = contents.split('\n')
lines.forEach((l, i, a) => {
const exportMatch = l.match(/^\s*export\s+const +([a-zA-Z0-9]+RE)/)
const exportMatch = l.match(/^\s*export\s+const +([a-zA-Z0-9]+RE(?=[ =]))/)
if (exportMatch !== null) {
const [, reName] = exportMatch
const prevLine = a[i - 1]
Expand Down
10 changes: 10 additions & 0 deletions src/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ const verified = emailRE.test(userInput)

## Regex reference

Each regular expression listed below is paired with an exported embeddable string named `xxxString`. E.g., `rgbRE` is paired with `rgbREString`. While the RE is pinned to the beginning and end of the value; i.e, the RE begins with '^' and ends with '$', the string does not. It is up to the user to identify token seperators when using the RE strings. E.g., if using `rgbREString` in a larger RE, one might do something like:

```javascript
import { rgbREString } from '@liquid-labs/regex-repo'

const allColors = cssContent
.matchAll(new RegExp(`[ :](${rgbREString})`)).map((match) => match[1])
.filter((v, i, arr) => i === arr.indexOf(v)) // filter non-unique items
.sort()
```
4 changes: 2 additions & 2 deletions src/ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

const uuidReString = '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
export const uuidREString = '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'
// Identifiers: Matches a UUID.
export const uuidRE = new RegExp(uuidReString, 'i')
export const uuidRE = new RegExp(uuidREString)
Loading