Convert lodah-es and d3 to named imports for presets

This commit is contained in:
Bryan Housel
2017-09-25 14:20:24 -04:00
parent 855c112ae0
commit d53e473474
5 changed files with 44 additions and 31 deletions

View File

@@ -1,10 +1,10 @@
import _ from 'lodash';
import _clone from 'lodash-es/clone';
import { t } from '../util/locale';
import { presetCollection } from './collection';
export function presetCategory(id, category, all) {
category = _.clone(category);
category = _clone(category);
category.id = id;

View File

@@ -1,4 +1,10 @@
import _ from 'lodash';
import _filter from 'lodash-es/filter';
import _find from 'lodash-es/find';
import _some from 'lodash-es/some';
import _uniq from 'lodash-es/uniq';
import _values from 'lodash-es/values';
import _without from 'lodash-es/without';
import { utilEditDistance } from '../util/index';
@@ -12,7 +18,7 @@ export function presetCollection(collection) {
item: function(id) {
return _.find(this.collection, function(d) {
return _find(this.collection, function(d) {
return d.id === id;
});
},
@@ -44,16 +50,16 @@ export function presetCollection(collection) {
value = value.toLowerCase();
var searchable = _.filter(this.collection, function(a) {
var searchable = _filter(this.collection, function(a) {
return a.searchable !== false && a.suggestion !== true;
}),
suggestions = _.filter(this.collection, function(a) {
suggestions = _filter(this.collection, function(a) {
return a.suggestion === true;
});
// matches value to preset.name
var leading_name = _.filter(searchable, function(a) {
var leading_name = _filter(searchable, function(a) {
return leading(a.name().toLowerCase());
}).sort(function(a, b) {
var aCompare = a.name().toLowerCase(),
@@ -77,13 +83,13 @@ export function presetCollection(collection) {
});
// matches value to preset.terms values
var leading_terms = _.filter(searchable, function(a) {
return _.some(a.terms() || [], leading);
var leading_terms = _filter(searchable, function(a) {
return _some(a.terms() || [], leading);
});
// matches value to preset.tags values
var leading_tag_values = _.filter(searchable, function(a) {
return _.some(_.without(_.values(a.tags || {}), '*'), leading);
var leading_tag_values = _filter(searchable, function(a) {
return _some(_without(_values(a.tags || {}), '*'), leading);
});
@@ -102,13 +108,13 @@ export function presetCollection(collection) {
});
// finds close matches to value in preset.terms
var similar_terms = _.filter(searchable, function(a) {
return _.some(a.terms() || [], function(b) {
var similar_terms = _filter(searchable, function(a) {
return _some(a.terms() || [], function(b) {
return utilEditDistance(value, b) + Math.min(value.length - b.length, 0) < 3;
});
});
var leading_suggestions = _.filter(suggestions, function(a) {
var leading_suggestions = _filter(suggestions, function(a) {
return leading(suggestionName(a.name()));
}).sort(function(a, b) {
a = suggestionName(a.name());
@@ -142,7 +148,7 @@ export function presetCollection(collection) {
similar_suggestions.slice(0, maxSuggestionResults)
).slice(0, maxSearchResults - 1);
return presetCollection(_.uniq(results.concat(other)));
return presetCollection(_uniq(results.concat(other)));
}
};

View File

@@ -1,9 +1,9 @@
import _ from 'lodash';
import _clone from 'lodash-es/clone';
import { t } from '../util/locale';
export function presetField(id, field) {
field = _.clone(field);
field = _clone(field);
field.id = id;

View File

@@ -1,4 +1,8 @@
import _ from 'lodash';
import _bind from 'lodash-es/bind';
import _forEach from 'lodash-es/forEach';
import _reject from 'lodash-es/reject';
import _uniq from 'lodash-es/uniq';
import { data } from '../../data/index';
import { presetCategory } from './category';
import { presetCollection } from './collection';
@@ -84,7 +88,7 @@ export function presetIndex() {
all.areaKeys = function() {
var areaKeys = {},
ignore = ['barrier', 'highway', 'footway', 'railway', 'type'], // probably a line..
presets = _.reject(all.collection, 'suggestion');
presets = _reject(all.collection, 'suggestion');
// whitelist
presets.forEach(function(d) {
@@ -125,26 +129,26 @@ export function presetIndex() {
index = { point: {}, vertex: {}, line: {}, area: {}, relation: {} };
if (d.fields) {
_.forEach(d.fields, function(d, id) {
_forEach(d.fields, function(d, id) {
fields[id] = presetField(id, d);
if (d.universal) universal.push(fields[id]);
});
}
if (d.presets) {
_.forEach(d.presets, function(d, id) {
_forEach(d.presets, function(d, id) {
all.collection.push(presetPreset(id, d, fields));
});
}
if (d.categories) {
_.forEach(d.categories, function(d, id) {
_forEach(d.categories, function(d, id) {
all.collection.push(presetCategory(id, d, all));
});
}
if (d.defaults) {
var getItem = _.bind(all.item, all);
var getItem = _bind(all.item, all);
defaults = {
area: presetCollection(d.defaults.area.map(getItem)),
line: presetCollection(d.defaults.line.map(getItem)),
@@ -179,13 +183,13 @@ export function presetIndex() {
all.defaults = function(geometry, n) {
var rec = recent.matchGeometry(geometry).collection.slice(0, 4),
def = _.uniq(rec.concat(defaults[geometry].collection)).slice(0, n - 1);
return presetCollection(_.uniq(rec.concat(def).concat(all.item(geometry))));
def = _uniq(rec.concat(defaults[geometry].collection)).slice(0, n - 1);
return presetCollection(_uniq(rec.concat(def).concat(all.item(geometry))));
};
all.choose = function(preset) {
if (!preset.isFallback()) {
recent = presetCollection(_.uniq([preset].concat(recent.collection)));
recent = presetCollection(_uniq([preset].concat(recent.collection)));
}
return all;
};

View File

@@ -1,10 +1,13 @@
import _ from 'lodash';
import _clone from 'lodash-es/clone';
import _keys from 'lodash-es/keys';
import _omit from 'lodash-es/omit';
import { t } from '../util/locale';
import { areaKeys } from '../core/context';
export function presetPreset(id, preset, fields) {
preset = _.clone(preset);
preset = _clone(preset);
preset.id = id;
preset.fields = (preset.fields || []).map(getFields);
@@ -71,7 +74,7 @@ export function presetPreset(id, preset, fields) {
var reference = preset.reference || {};
preset.reference = function(geometry) {
var key = reference.key || Object.keys(_.omit(preset.tags, 'name'))[0],
var key = reference.key || Object.keys(_omit(preset.tags, 'name'))[0],
value = reference.value || preset.tags[key];
if (geometry === 'relation' && key === 'type') {
@@ -93,7 +96,7 @@ export function presetPreset(id, preset, fields) {
var removeTags = preset.removeTags || preset.tags;
preset.removeTags = function(tags, geometry) {
tags = _.omit(tags, _.keys(removeTags));
tags = _omit(tags, _keys(removeTags));
for (var f in preset.fields) {
var field = preset.fields[f];
@@ -111,7 +114,7 @@ export function presetPreset(id, preset, fields) {
preset.applyTags = function(tags, geometry) {
var k;
tags = _.clone(tags);
tags = _clone(tags);
for (k in applyTags) {
if (applyTags[k] === '*') {