File: index.js

package info (click to toggle)
node-unicode-loose-match 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 192 kB
  • sloc: makefile: 24; sh: 7
file content (40 lines) | stat: -rwxr-xr-x 1,076 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';

const mappings = require('./data/mappings.js');
const aliasToProperty = mappings.aliasToProperty;
const propertyToValueAliases = mappings.propertyToValueAliases;

const normalize = function(string) {
	const normalized = string
		// Remove case distinctions.
		.toLowerCase()
		// Remove whitespace.
		.replace(/\s/g, '')
		// Remove `-` and `_`.
		.replace(/[-_]/g, '');
	return normalized;
};

const matchLoosely = function(property, value) {
	const normalizedProperty = normalize(property);
	const canonicalProperty = aliasToProperty.get(normalizedProperty);
	if (!canonicalProperty) {
		throw new Error(`Unknown property: ${ property }`);
	}
	const aliasToValue = propertyToValueAliases.get(
		canonicalProperty == 'Script_Extensions' ? 'Script' : canonicalProperty
	);
	const result = {
		'property': canonicalProperty
	};
	if (value) {
		const normalizedValue = normalize(value);
		const canonicalValue = aliasToValue.get(normalizedValue);
		if (canonicalValue) {
			result.value = canonicalValue;
		}
	}
	return result;
};

module.exports = matchLoosely;