@atlaskit/codemod-cli 0.8.1 → 0.8.5
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/CHANGELOG.md +26 -0
- package/README.md +12 -2
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/index.js +4 -4
- package/dist/cjs/main.js +6 -4
- package/dist/cjs/presets/index.js +4 -2
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +145 -0
- package/dist/cjs/presets/theme-to-design-tokens/types.js +5 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +88 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +19 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +59 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +348 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/legacy-colors.js +83 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/named-colors.js +8 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +38 -0
- package/dist/cjs/transforms.js +1 -1
- package/dist/cjs/types.js +5 -3
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/cli.js +1 -1
- package/dist/es2019/main.js +3 -1
- package/dist/es2019/presets/index.js +2 -1
- package/dist/es2019/presets/theme-to-design-tokens/theme-to-design-tokens.js +100 -0
- package/dist/es2019/presets/theme-to-design-tokens/types.js +1 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast-meta.js +63 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast.js +10 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/color.js +35 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/fuzzy-search.js +336 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/legacy-colors.js +74 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/named-colors.js +1 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/tokens.js +12 -0
- package/dist/es2019/version.json +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/main.js +6 -4
- package/dist/esm/presets/index.js +3 -2
- package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +130 -0
- package/dist/esm/presets/theme-to-design-tokens/types.js +1 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/ast-meta.js +75 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/ast.js +10 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/color.js +39 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/fuzzy-search.js +340 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/legacy-colors.js +74 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/named-colors.js +1 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/tokens.js +24 -0
- package/dist/esm/types.js +3 -2
- package/dist/esm/version.json +1 -1
- package/dist/types/presets/index.d.ts +1 -0
- package/dist/types/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +2 -0
- package/dist/types/presets/theme-to-design-tokens/utils/ast-meta.d.ts +3 -0
- package/dist/types/presets/theme-to-design-tokens/utils/ast.d.ts +3 -0
- package/dist/types/presets/theme-to-design-tokens/utils/color.d.ts +4 -0
- package/dist/types/presets/theme-to-design-tokens/utils/fuzzy-search.d.ts +5 -0
- package/dist/types/presets/theme-to-design-tokens/utils/legacy-colors.d.ts +3 -0
- package/dist/types/presets/theme-to-design-tokens/utils/named-colors.d.ts +1 -0
- package/dist/types/presets/theme-to-design-tokens/utils/tokens.d.ts +2 -0
- package/package.json +4 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
import { isDecendantOfToken, isDecendantOfType } from './utils/ast';
|
|
5
|
+
import { cleanMeta, getMetaFromAncestors } from './utils/ast-meta';
|
|
6
|
+
import { includesHardCodedColor, isHardCodedColor, isLegacyColor, isLegacyNamedColor } from './utils/color';
|
|
7
|
+
import Search from './utils/fuzzy-search';
|
|
8
|
+
import { legacyColorMetaMap } from './utils/legacy-colors';
|
|
9
|
+
import { tokens } from './utils/tokens';
|
|
10
|
+
var search = Search(tokens, false);
|
|
11
|
+
|
|
12
|
+
function hasImportDeclaration(j, source, sourcePath) {
|
|
13
|
+
return !!source.find(j.ImportDeclaration).filter(function (path) {
|
|
14
|
+
return path.node.source.value === sourcePath;
|
|
15
|
+
}).length;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
19
|
+
return !!source.find(j.ImportDeclaration).filter(function (path) {
|
|
20
|
+
return path.node.source.value === sourcePath;
|
|
21
|
+
}).find(j.ImportSpecifier, {
|
|
22
|
+
local: {
|
|
23
|
+
name: specifier
|
|
24
|
+
}
|
|
25
|
+
}).length;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function insertTokenImport(j, source) {
|
|
29
|
+
if (hasImportDeclaration(j, source, '@atlaskit/tokens')) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
34
|
+
source.get().node.program.body.unshift(newImport);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function buildToken(j, tokenId, node) {
|
|
38
|
+
var callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
39
|
+
return callExpr;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getTokenFromNode(j, path) {
|
|
43
|
+
var baseMeta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
44
|
+
var foundMeta = getMetaFromAncestors(j, path);
|
|
45
|
+
var meta = cleanMeta([].concat(_toConsumableArray(foundMeta), _toConsumableArray(baseMeta)));
|
|
46
|
+
var results = search.get(meta.join(' '));
|
|
47
|
+
var tokenId = ['utility.UNSAFE_util.MISSING_TOKEN'];
|
|
48
|
+
|
|
49
|
+
if (results) {
|
|
50
|
+
tokenId = results.map(function (result) {
|
|
51
|
+
return result[1];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return tokenId[0];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default function transformer(file, api) {
|
|
59
|
+
var debug = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
60
|
+
var j = api.jscodeshift;
|
|
61
|
+
var source = j(file.source);
|
|
62
|
+
var transformed = false;
|
|
63
|
+
source // Handle colors.N100
|
|
64
|
+
.find(j.MemberExpression).filter(function (path) {
|
|
65
|
+
return path.value.object.type === 'Identifier' && path.value.object.name === 'colors' && path.value.property.type === 'Identifier' && isLegacyColor(path.value.property.name);
|
|
66
|
+
}).filter(function (path) {
|
|
67
|
+
return !isDecendantOfToken(j, path);
|
|
68
|
+
}).forEach(function (path) {
|
|
69
|
+
debug && console.log('file:', file.path);
|
|
70
|
+
insertTokenImport(j, source);
|
|
71
|
+
var key = path.value.property.type === 'Identifier' ? path.value.property.name : undefined;
|
|
72
|
+
var colorMeta = legacyColorMetaMap[key] || [];
|
|
73
|
+
var tokenId = getTokenFromNode(j, path, colorMeta);
|
|
74
|
+
j(path).replaceWith(buildToken(j, tokenId, path.value));
|
|
75
|
+
transformed = true;
|
|
76
|
+
});
|
|
77
|
+
source.find(j.ObjectProperty).filter(function (path) {
|
|
78
|
+
return path.value.value.type === 'Identifier' && (isLegacyColor(path.value.value.name) || isLegacyNamedColor(path.value.value.name));
|
|
79
|
+
}).filter(function (path) {
|
|
80
|
+
return hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme/colors');
|
|
81
|
+
}).filter(function (path) {
|
|
82
|
+
return !isDecendantOfToken(j, path.value.value);
|
|
83
|
+
}).forEach(function (path) {
|
|
84
|
+
var valuePath = path.get('value');
|
|
85
|
+
debug && console.log('file:', file.path);
|
|
86
|
+
insertTokenImport(j, source);
|
|
87
|
+
var colorMeta = legacyColorMetaMap[valuePath.name] || [];
|
|
88
|
+
var tokenId = getTokenFromNode(j, valuePath, colorMeta);
|
|
89
|
+
j(path).replaceWith(j.objectProperty(path.value.key, buildToken(j, tokenId, valuePath.value)));
|
|
90
|
+
transformed = true;
|
|
91
|
+
});
|
|
92
|
+
source.find(j.Identifier).filter(function (path) {
|
|
93
|
+
return isLegacyColor(path.value.name) || isLegacyNamedColor(path.value.name);
|
|
94
|
+
}).filter(function (path) {
|
|
95
|
+
return hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme/colors');
|
|
96
|
+
}).filter(function (path) {
|
|
97
|
+
return !['ImportSpecifier', 'MemberExpression', 'ObjectProperty'].includes(path.parentPath.value.type);
|
|
98
|
+
}).filter(function (path) {
|
|
99
|
+
return !isDecendantOfToken(j, path);
|
|
100
|
+
}).forEach(function (path) {
|
|
101
|
+
debug && console.log('file:', file.path);
|
|
102
|
+
insertTokenImport(j, source);
|
|
103
|
+
var colorMeta = legacyColorMetaMap[path.value.name] || [];
|
|
104
|
+
var tokenId = getTokenFromNode(j, path, colorMeta);
|
|
105
|
+
j(path).replaceWith(buildToken(j, tokenId, path.value));
|
|
106
|
+
transformed = true;
|
|
107
|
+
});
|
|
108
|
+
source.find(j.Literal).filter(function (path) {
|
|
109
|
+
return typeof path.value.value === 'string' && (includesHardCodedColor(path.value.value) || isHardCodedColor(path.value.value));
|
|
110
|
+
}).filter(function (path) {
|
|
111
|
+
return !isDecendantOfToken(j, path);
|
|
112
|
+
}).forEach(function (path) {
|
|
113
|
+
var _path$value, _path$value$value;
|
|
114
|
+
|
|
115
|
+
debug && console.log('file:', file.path);
|
|
116
|
+
insertTokenImport(j, source);
|
|
117
|
+
var value = path === null || path === void 0 ? void 0 : (_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$value = _path$value.value) === null || _path$value$value === void 0 ? void 0 : _path$value$value.toString();
|
|
118
|
+
var colorMeta = legacyColorMetaMap[value] || [];
|
|
119
|
+
var tokenId = getTokenFromNode(j, path, colorMeta);
|
|
120
|
+
var tokenNode = buildToken(j, tokenId, path.value);
|
|
121
|
+
j(path).replaceWith(isDecendantOfType(j, path, j.JSXAttribute) ? j.jsxExpressionContainer(tokenNode) : tokenNode);
|
|
122
|
+
transformed = true;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (transformed) {
|
|
126
|
+
return source.toSource();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return file.source;
|
|
130
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import { getUniqueWordsFromTokens } from './tokens';
|
|
3
|
+
export function getMetaFromAncestors(j, path) {
|
|
4
|
+
var meta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
5
|
+
var parent = path.parentPath;
|
|
6
|
+
var grandParent = parent && parent.parentPath;
|
|
7
|
+
|
|
8
|
+
if (parent && parent.value.type === 'ObjectProperty') {
|
|
9
|
+
var value = '';
|
|
10
|
+
|
|
11
|
+
if (parent.value.key.type === 'Literal' || parent.value.key.type === 'StringLiteral' || parent.value.key.type === 'NumericLiteral') {
|
|
12
|
+
value = parent.value.key.value.toString();
|
|
13
|
+
} else {
|
|
14
|
+
value = parent.value.key.name;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
meta.push(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (parent && grandParent && grandParent.value.type === 'TemplateLiteral') {
|
|
21
|
+
var expressionIndex = grandParent.value.expressions.findIndex(function (exp) {
|
|
22
|
+
return exp.name === path.value.name;
|
|
23
|
+
});
|
|
24
|
+
var quasi = grandParent.value.quasis[expressionIndex];
|
|
25
|
+
var propertyName = (quasi.value.cooked || quasi.value.raw || '').replace(/\n/g, '').split(/;|{|}/).filter(function (el) {
|
|
26
|
+
return !el.match(/\.|\@|\(|\)/);
|
|
27
|
+
}).pop().split(/:/g)[0].trim();
|
|
28
|
+
grandParent.value.quasis.slice(0, expressionIndex + 1).map(function (q) {
|
|
29
|
+
return q.value.cooked;
|
|
30
|
+
}) // We reverse so the most nested one is first which we're more likely than not interested in
|
|
31
|
+
.reverse().some(function (str) {
|
|
32
|
+
var result = /(hover|active|disabled|focus)/.exec(str.toLowerCase());
|
|
33
|
+
|
|
34
|
+
if (result) {
|
|
35
|
+
meta.push(result[0]);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
meta.push(propertyName);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (parent && parent.value.type === 'JSXAttribute') {
|
|
43
|
+
if (!['css', 'styles', 'style'].includes(parent.value.name.name)) {
|
|
44
|
+
meta.push(parent.value.name.name);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (parent && parent.value.type === 'VariableDeclarator') {
|
|
49
|
+
meta.push(parent.value.id.name);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (parent) {
|
|
53
|
+
return getMetaFromAncestors(j, parent, meta);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return meta;
|
|
57
|
+
}
|
|
58
|
+
export function cleanMeta(meta) {
|
|
59
|
+
return meta.reduce(function (accum, val) {
|
|
60
|
+
return [].concat(_toConsumableArray(accum), _toConsumableArray(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(function (e) {
|
|
61
|
+
return e.toLowerCase();
|
|
62
|
+
}) : []));
|
|
63
|
+
}, []).reduce(function (accum, val) {
|
|
64
|
+
accum.push(val.replace(/:/g, '').replace(/,/g, '').replace('grey', 'neutral').replace('skeleton', 'neutral').replace('texts', 'text').replace('red', 'danger').replace('error', 'danger').replace('invalid', 'danger').replace('removed', 'danger').replace('removal', 'danger').replace('remove', 'danger').replace('focus', 'focused').replace('valid', 'success').replace('successful', 'success').replace('risk', 'warning').replace('caution', 'warning').replace('warn', 'warning').replace('primary', 'bold').replace('info', 'bold').replace('secondary', 'subtle').replace('hyperlink', 'link').replace('anchor', 'link').replace('active', 'pressed').replace('hover', 'hovered').replace('dragged', 'overlay').replace('dragging', 'overlay').replace('drag', 'overlay').replace('background-color', 'background').replace('color', 'text').replace('icons', 'icon').replace('glyph', 'icon').replace('stroke', 'border').replace('border-left', 'border').replace('border-right', 'border').replace('border-top', 'border').replace('border-bottom', 'border').replace('box-shadow', 'shadow'));
|
|
65
|
+
return accum;
|
|
66
|
+
}, []).filter(function (val) {
|
|
67
|
+
return getUniqueWordsFromTokens.includes(val);
|
|
68
|
+
}).reduce(function (accum, val) {
|
|
69
|
+
if (!accum.includes(val)) {
|
|
70
|
+
accum.push(val);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return accum;
|
|
74
|
+
}, []);
|
|
75
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { legacyColorMixins, legacyColors } from './legacy-colors';
|
|
2
|
+
import { namedColors } from './named-colors';
|
|
3
|
+
export var isLegacyColor = function isLegacyColor(value) {
|
|
4
|
+
return legacyColors.includes(value);
|
|
5
|
+
};
|
|
6
|
+
export var isLegacyNamedColor = function isLegacyNamedColor(value) {
|
|
7
|
+
return legacyColorMixins.includes(value);
|
|
8
|
+
};
|
|
9
|
+
export var includesHardCodedColor = function includesHardCodedColor(raw) {
|
|
10
|
+
var value = raw.toLowerCase();
|
|
11
|
+
|
|
12
|
+
if (/#(?:[a-f0-9]{3}|[a-f0-9]{6}|[a-f0-9]{8})\b|(?:rgb|hsl)a?\([^\)]*\)/.exec(value.toLowerCase())) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (var i = 0; i < namedColors.length; i++) {
|
|
17
|
+
if (value.includes("".concat(namedColors[i], ";"))) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return false;
|
|
23
|
+
};
|
|
24
|
+
export var isHardCodedColor = function isHardCodedColor(value) {
|
|
25
|
+
if (namedColors.includes(value.toLowerCase())) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (value.startsWith('rgb(') || value.startsWith('rgba(') || value.startsWith('hsl(') || value.startsWith('hsla(') || value.startsWith('lch(') || value.startsWith('lab(') || value.startsWith('color(')) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (value.startsWith('#') && ( // short hex, hex, or hex with alpha
|
|
34
|
+
value.length === 4 || value.length === 7 || value.length === 9)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fuzzy search ripped from the internet.
|
|
5
|
+
*/
|
|
6
|
+
var FuzzySet = function FuzzySet() {
|
|
7
|
+
var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
8
|
+
var useLevenshtein = arguments.length > 1 ? arguments[1] : undefined;
|
|
9
|
+
var gramSizeLower = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
|
|
10
|
+
var gramSizeUpper = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 3;
|
|
11
|
+
var fuzzyset = {
|
|
12
|
+
gramSizeLower: gramSizeLower,
|
|
13
|
+
gramSizeUpper: gramSizeUpper,
|
|
14
|
+
useLevenshtein: typeof useLevenshtein !== 'boolean' ? true : useLevenshtein,
|
|
15
|
+
exactSet: {},
|
|
16
|
+
matchDict: {},
|
|
17
|
+
items: {}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var levenshtein = function levenshtein(str1, str2) {
|
|
21
|
+
var current = [];
|
|
22
|
+
var prev;
|
|
23
|
+
var value;
|
|
24
|
+
|
|
25
|
+
for (var i = 0; i <= str2.length; i++) {
|
|
26
|
+
for (var j = 0; j <= str1.length; j++) {
|
|
27
|
+
if (i && j) {
|
|
28
|
+
if (str1.charAt(j - 1) === str2.charAt(i - 1)) {
|
|
29
|
+
// @ts-expect-error
|
|
30
|
+
value = prev;
|
|
31
|
+
} else {
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
value = Math.min(current[j], current[j - 1], prev) + 1;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
value = i + j;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
prev = current[j];
|
|
40
|
+
current[j] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return current.pop();
|
|
45
|
+
}; // return an edit distance from 0 to 1
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
var _distance = function _distance(str1, str2) {
|
|
49
|
+
if (str1 === null && str2 === null) {
|
|
50
|
+
throw new Error('Trying to compare two null values');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (str1 === null || str2 === null) {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
str1 = String(str1);
|
|
58
|
+
str2 = String(str2);
|
|
59
|
+
var distance = levenshtein(str1, str2);
|
|
60
|
+
|
|
61
|
+
if (str1.length > str2.length) {
|
|
62
|
+
return 1 - distance / str1.length;
|
|
63
|
+
} else {
|
|
64
|
+
return 1 - distance / str2.length;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
var _nonWordRe = /[^a-zA-Z0-9\u00C0-\u00FF, ]+/g;
|
|
69
|
+
|
|
70
|
+
var _iterateGrams = function _iterateGrams(value, gramSize) {
|
|
71
|
+
gramSize = gramSize || 2;
|
|
72
|
+
var simplified = '-' + value.toLowerCase().replace(_nonWordRe, '') + '-',
|
|
73
|
+
lenDiff = gramSize - simplified.length,
|
|
74
|
+
results = [];
|
|
75
|
+
|
|
76
|
+
if (lenDiff > 0) {
|
|
77
|
+
for (var i = 0; i < lenDiff; ++i) {
|
|
78
|
+
simplified += '-';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (var i = 0; i < simplified.length - gramSize + 1; ++i) {
|
|
83
|
+
results.push(simplified.slice(i, i + gramSize));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return results;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
var _gramCounter = function _gramCounter(value, gramSize) {
|
|
90
|
+
// return an object where key=gram, value=number of occurrences
|
|
91
|
+
gramSize = gramSize || 2;
|
|
92
|
+
|
|
93
|
+
var result = {},
|
|
94
|
+
grams = _iterateGrams(value, gramSize),
|
|
95
|
+
i = 0;
|
|
96
|
+
|
|
97
|
+
for (i; i < grams.length; ++i) {
|
|
98
|
+
if (grams[i] in result) {
|
|
99
|
+
// @ts-expect-error
|
|
100
|
+
result[grams[i]] += 1;
|
|
101
|
+
} else {
|
|
102
|
+
// @ts-expect-error
|
|
103
|
+
result[grams[i]] = 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return result;
|
|
108
|
+
}; // the main functions
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
fuzzyset.get = function (value, defaultValue, minMatchScore) {
|
|
112
|
+
// check for value in set, returning defaultValue or null if none found
|
|
113
|
+
if (minMatchScore === undefined) {
|
|
114
|
+
minMatchScore = 0.33;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
var result = this._get(value, minMatchScore);
|
|
118
|
+
|
|
119
|
+
if (!result && typeof defaultValue !== 'undefined') {
|
|
120
|
+
return defaultValue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return result;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
fuzzyset._get = function (value, minMatchScore) {
|
|
127
|
+
var results = []; // start with high gram size and if there are no results, go to lower gram sizes
|
|
128
|
+
|
|
129
|
+
for (var gramSize = this.gramSizeUpper; gramSize >= this.gramSizeLower; --gramSize) {
|
|
130
|
+
results = this.__get(value, gramSize, minMatchScore);
|
|
131
|
+
|
|
132
|
+
if (results && results.length > 0) {
|
|
133
|
+
return results;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return null;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
fuzzyset.__get = function (value, gramSize, minMatchScore) {
|
|
141
|
+
var normalizedValue = this._normalizeStr(value),
|
|
142
|
+
matches = {},
|
|
143
|
+
gramCounts = _gramCounter(normalizedValue, gramSize),
|
|
144
|
+
items = this.items[gramSize],
|
|
145
|
+
sumOfSquareGramCounts = 0,
|
|
146
|
+
gram,
|
|
147
|
+
gramCount,
|
|
148
|
+
i,
|
|
149
|
+
index,
|
|
150
|
+
otherGramCount;
|
|
151
|
+
|
|
152
|
+
for (gram in gramCounts) {
|
|
153
|
+
// @ts-expect-error
|
|
154
|
+
gramCount = gramCounts[gram];
|
|
155
|
+
sumOfSquareGramCounts += Math.pow(gramCount, 2);
|
|
156
|
+
|
|
157
|
+
if (gram in this.matchDict) {
|
|
158
|
+
for (i = 0; i < this.matchDict[gram].length; ++i) {
|
|
159
|
+
index = this.matchDict[gram][i][0];
|
|
160
|
+
otherGramCount = this.matchDict[gram][i][1];
|
|
161
|
+
|
|
162
|
+
if (index in matches) {
|
|
163
|
+
// @ts-expect-error
|
|
164
|
+
matches[index] += gramCount * otherGramCount;
|
|
165
|
+
} else {
|
|
166
|
+
// @ts-expect-error
|
|
167
|
+
matches[index] = gramCount * otherGramCount;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function isEmptyObject(obj) {
|
|
174
|
+
for (var prop in obj) {
|
|
175
|
+
if (obj.hasOwnProperty(prop)) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (isEmptyObject(matches)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
var vectorNormal = Math.sqrt(sumOfSquareGramCounts),
|
|
188
|
+
results = [],
|
|
189
|
+
matchScore; // build a results list of [score, str]
|
|
190
|
+
|
|
191
|
+
for (var matchIndex in matches) {
|
|
192
|
+
// @ts-expect-error
|
|
193
|
+
matchScore = matches[matchIndex];
|
|
194
|
+
results.push([matchScore / (vectorNormal * items[matchIndex][0]), items[matchIndex][1]]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
var sortDescending = function sortDescending(a, b) {
|
|
198
|
+
if (a[0] < b[0]) {
|
|
199
|
+
return 1;
|
|
200
|
+
} else if (a[0] > b[0]) {
|
|
201
|
+
return -1;
|
|
202
|
+
} else {
|
|
203
|
+
return 0;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
results.sort(sortDescending);
|
|
208
|
+
|
|
209
|
+
if (this.useLevenshtein) {
|
|
210
|
+
var newResults = [],
|
|
211
|
+
endIndex = Math.min(50, results.length); // truncate somewhat arbitrarily to 50
|
|
212
|
+
// @ts-expect-error
|
|
213
|
+
|
|
214
|
+
for (var i = 0; i < endIndex; ++i) {
|
|
215
|
+
// @ts-expect-error
|
|
216
|
+
newResults.push([_distance(results[i][1], normalizedValue), results[i][1]]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
results = newResults;
|
|
220
|
+
results.sort(sortDescending);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
newResults = [];
|
|
224
|
+
results.forEach(function (scoreWordPair) {
|
|
225
|
+
if (scoreWordPair[0] >= minMatchScore) {
|
|
226
|
+
// @ts-expect-error
|
|
227
|
+
newResults.push([scoreWordPair[0], this.exactSet[scoreWordPair[1]]]);
|
|
228
|
+
}
|
|
229
|
+
}.bind(this));
|
|
230
|
+
return newResults;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
fuzzyset.add = function (value) {
|
|
234
|
+
var normalizedValue = this._normalizeStr(value);
|
|
235
|
+
|
|
236
|
+
if (normalizedValue in this.exactSet) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
var i = this.gramSizeLower;
|
|
241
|
+
|
|
242
|
+
for (i; i < this.gramSizeUpper + 1; ++i) {
|
|
243
|
+
this._add(value, i);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
fuzzyset._add = function (value, gramSize) {
|
|
248
|
+
var normalizedValue = this._normalizeStr(value),
|
|
249
|
+
items = this.items[gramSize] || [],
|
|
250
|
+
index = items.length;
|
|
251
|
+
|
|
252
|
+
items.push(0);
|
|
253
|
+
|
|
254
|
+
var gramCounts = _gramCounter(normalizedValue, gramSize);
|
|
255
|
+
|
|
256
|
+
var sumOfSquareGramCounts = 0;
|
|
257
|
+
var gram;
|
|
258
|
+
var gramCount;
|
|
259
|
+
|
|
260
|
+
for (gram in gramCounts) {
|
|
261
|
+
// @ts-expect-error
|
|
262
|
+
gramCount = gramCounts[gram];
|
|
263
|
+
sumOfSquareGramCounts += Math.pow(gramCount, 2);
|
|
264
|
+
|
|
265
|
+
if (gram in this.matchDict) {
|
|
266
|
+
this.matchDict[gram].push([index, gramCount]);
|
|
267
|
+
} else {
|
|
268
|
+
this.matchDict[gram] = [[index, gramCount]];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var vectorNormal = Math.sqrt(sumOfSquareGramCounts);
|
|
273
|
+
items[index] = [vectorNormal, normalizedValue];
|
|
274
|
+
this.items[gramSize] = items;
|
|
275
|
+
this.exactSet[normalizedValue] = value;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
fuzzyset._normalizeStr = function (str) {
|
|
279
|
+
if (Object.prototype.toString.call(str) !== '[object String]') {
|
|
280
|
+
throw new Error('Must use a string as argument to FuzzySet functions');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return str.toLowerCase();
|
|
284
|
+
}; // return length of items in set
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
fuzzyset.length = function () {
|
|
288
|
+
var count = 0,
|
|
289
|
+
prop;
|
|
290
|
+
|
|
291
|
+
for (prop in this.exactSet) {
|
|
292
|
+
if (this.exactSet.hasOwnProperty(prop)) {
|
|
293
|
+
count += 1;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return count;
|
|
298
|
+
}; // return is set is empty
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
fuzzyset.isEmpty = function () {
|
|
302
|
+
for (var prop in this.exactSet) {
|
|
303
|
+
if (this.exactSet.hasOwnProperty(prop)) {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return true;
|
|
309
|
+
}; // return list of values loaded into set
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
fuzzyset.values = function () {
|
|
313
|
+
var values = [],
|
|
314
|
+
prop;
|
|
315
|
+
|
|
316
|
+
for (prop in this.exactSet) {
|
|
317
|
+
if (this.exactSet.hasOwnProperty(prop)) {
|
|
318
|
+
values.push(this.exactSet[prop]);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return values;
|
|
323
|
+
}; // initialization
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
var i = fuzzyset.gramSizeLower;
|
|
327
|
+
|
|
328
|
+
for (i; i < fuzzyset.gramSizeUpper + 1; ++i) {
|
|
329
|
+
fuzzyset.items[i] = [];
|
|
330
|
+
} // add all the items to the set
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
for (i = 0; i < arr.length; ++i) {
|
|
334
|
+
fuzzyset.add(arr[i]);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return fuzzyset;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
export default FuzzySet;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export var legacyColors = ['R50', 'R75', 'R100', 'R200', 'R300', 'R400', 'R500', 'Y50', 'Y75', 'Y100', 'Y200', 'Y300', 'Y400', 'Y500', 'G50', 'G75', 'G100', 'G200', 'G300', 'G400', 'G500', 'B50', 'B75', 'B100', 'B200', 'B300', 'B400', 'B500', 'P50', 'P75', 'P100', 'P200', 'P300', 'P400', 'P500', 'T50', 'T75', 'T100', 'T200', 'T300', 'T400', 'T500', 'N0', 'N10', 'N20', 'N30', 'N40', 'N50', 'N60', 'N70', 'N80', 'N90', 'N100', 'N200', 'N300', 'N400', 'N500', 'N600', 'N700', 'N800', 'N900', 'N10A', 'N20A', 'N30A', 'N40A', 'N50A', 'N60A', 'N70A', 'N80A', 'N90A', 'N100A', 'N200A', 'N300A', 'N400A', 'N500A', 'N600A', 'N700A', 'N800A', 'DN900', 'DN800', 'DN700', 'DN600', 'DN500', 'DN400', 'DN300', 'DN200', 'DN100', 'DN90', 'DN80', 'DN70', 'DN60', 'DN50', 'DN40', 'DN30', 'DN20', 'DN10', 'DN0', 'DN800A', 'DN700A', 'DN600A', 'DN500A', 'DN400A', 'DN300A', 'DN200A', 'DN100A', 'DN90A', 'DN80A', 'DN70A', 'DN60A', 'DN50A', 'DN40A', 'DN30A', 'DN20A', 'DN10A'];
|
|
2
|
+
export var legacyColorMixins = ['background', 'backgroundActive', 'backgroundHover', 'backgroundOnLayer', 'text', 'textHover', 'textActive', 'subtleText', 'placeholderText', 'heading', 'subtleHeading', 'codeBlock', 'link', 'linkHover', 'linkActive', 'linkOutline', 'primary', 'blue', 'teal', 'purple', 'red', 'yellow', 'green', 'skeleton'];
|
|
3
|
+
export var legacyColorMetaMap = {
|
|
4
|
+
R50: ['danger'],
|
|
5
|
+
R75: ['danger'],
|
|
6
|
+
R100: ['danger'],
|
|
7
|
+
R200: ['danger'],
|
|
8
|
+
R300: ['danger'],
|
|
9
|
+
R400: ['danger'],
|
|
10
|
+
R500: ['danger'],
|
|
11
|
+
Y50: ['warning'],
|
|
12
|
+
Y75: ['warning'],
|
|
13
|
+
Y100: ['warning'],
|
|
14
|
+
Y200: ['warning'],
|
|
15
|
+
Y300: ['warning'],
|
|
16
|
+
Y400: ['warning'],
|
|
17
|
+
Y500: ['warning'],
|
|
18
|
+
G50: ['success'],
|
|
19
|
+
G75: ['success'],
|
|
20
|
+
G100: ['success'],
|
|
21
|
+
G200: ['success'],
|
|
22
|
+
G300: ['success'],
|
|
23
|
+
G400: ['success'],
|
|
24
|
+
G500: ['success'],
|
|
25
|
+
B50: ['brand'],
|
|
26
|
+
B75: ['brand'],
|
|
27
|
+
B100: ['brand'],
|
|
28
|
+
B200: ['brand'],
|
|
29
|
+
B300: ['brand'],
|
|
30
|
+
B400: ['brand'],
|
|
31
|
+
B500: ['brand'],
|
|
32
|
+
P50: ['discovery'],
|
|
33
|
+
P75: ['discovery'],
|
|
34
|
+
P100: ['discovery'],
|
|
35
|
+
P200: ['discovery'],
|
|
36
|
+
P300: ['discovery'],
|
|
37
|
+
P400: ['discovery'],
|
|
38
|
+
P500: ['discovery'],
|
|
39
|
+
T50: ['accent', 'teal'],
|
|
40
|
+
T75: ['accent', 'teal'],
|
|
41
|
+
T100: ['accent', 'teal'],
|
|
42
|
+
T200: ['accent', 'teal'],
|
|
43
|
+
T300: ['accent', 'teal'],
|
|
44
|
+
T400: ['accent', 'teal'],
|
|
45
|
+
T500: ['accent', 'teal'],
|
|
46
|
+
N0: ['inverse'],
|
|
47
|
+
N700: ['text'],
|
|
48
|
+
N800: ['text'],
|
|
49
|
+
N900: ['text'],
|
|
50
|
+
background: ['background', 'default'],
|
|
51
|
+
backgroundActive: ['background', 'pressed'],
|
|
52
|
+
backgroundHover: ['background', 'hovered'],
|
|
53
|
+
backgroundOnLayer: ['background', 'blanket'],
|
|
54
|
+
text: ['text'],
|
|
55
|
+
textHover: ['text', 'subtle'],
|
|
56
|
+
textActive: ['text', 'link', 'pressed'],
|
|
57
|
+
subtleText: ['text', 'subtlest'],
|
|
58
|
+
placeholderText: ['text', 'subtlest'],
|
|
59
|
+
heading: ['text'],
|
|
60
|
+
subtleHeading: ['text', 'subtle'],
|
|
61
|
+
link: ['link'],
|
|
62
|
+
linkHover: ['link', 'hovered'],
|
|
63
|
+
linkActive: ['link', 'pressed'],
|
|
64
|
+
linkOutline: ['border', 'selected'],
|
|
65
|
+
primary: ['brand'],
|
|
66
|
+
blue: ['accent', 'blue'],
|
|
67
|
+
teal: ['accent', 'teal'],
|
|
68
|
+
purple: ['accent', 'purple'],
|
|
69
|
+
red: ['accent', 'red'],
|
|
70
|
+
yellow: ['accent', 'orange'],
|
|
71
|
+
green: ['accent', 'green'],
|
|
72
|
+
grey: ['background', 'neutral'],
|
|
73
|
+
skeleton: ['background', 'neutral']
|
|
74
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export var namedColors = ['black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua', 'orange', 'aliceblue', 'antiquewhite', 'aquamarine', 'azure', 'beige', 'bisque', 'blanchedalmond', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'limegreen', 'linen', 'magenta', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'oldlace', 'olivedrab', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'whitesmoke', 'yellowgreen', 'rebeccapurple'];
|