Skip to content

Commit 0eec2b0

Browse files
authored
Replace identifier library with nanoid (#1318)
1 parent c99763c commit 0eec2b0

File tree

8 files changed

+77
-20
lines changed

8 files changed

+77
-20
lines changed

package-lock.json

+42-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"@mapbox/geojson-normalize": "^0.0.1",
7272
"@mapbox/point-geometry": "^0.1.0",
7373
"fast-deep-equal": "^3.1.3",
74-
"hat": "0.0.3"
74+
"nanoid": "^5.0.9"
7575
},
7676
"files": [
7777
"src",

src/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import isEqual from 'fast-deep-equal';
22
import normalize from '@mapbox/geojson-normalize';
3-
import hat from 'hat';
3+
import {generateID} from './lib/id.js';
44
import featuresAt from './lib/features_at.js';
55
import stringSetsAreEqual from './lib/string_sets_are_equal.js';
66
import * as Constants from './constants.js';
@@ -78,7 +78,7 @@ export default function(ctx, api) {
7878
const featureCollection = JSON.parse(JSON.stringify(normalize(geojson)));
7979

8080
const ids = featureCollection.features.map((feature) => {
81-
feature.id = feature.id || hat();
81+
feature.id = feature.id || generateID();
8282

8383
if (feature.geometry === null) {
8484
throw new Error('Invalid geometry: null');

src/feature_types/feature.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import hat from 'hat';
1+
import {generateID} from '../lib/id.js';
22
import * as Constants from '../constants.js';
33

44
const Feature = function(ctx, geojson) {
55
this.ctx = ctx;
66
this.properties = geojson.properties || {};
77
this.coordinates = geojson.geometry.coordinates;
8-
this.id = geojson.id || hat();
8+
this.id = geojson.id || generateID();
99
this.type = geojson.geometry.type;
1010
};
1111

src/feature_types/multi_feature.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import hat from 'hat';
1+
import {generateID} from '../lib/id.js';
22
import Feature from './feature.js';
33
import * as Constants from '../constants.js';
44

@@ -33,7 +33,7 @@ MultiFeature.prototype = Object.create(Feature.prototype);
3333
MultiFeature.prototype._coordinatesToFeatures = function(coordinates) {
3434
const Model = this.model.bind(this);
3535
return coordinates.map(coords => new Model(this.ctx, {
36-
id: hat(),
36+
id: generateID(),
3737
type: Constants.geojsonTypes.FEATURE,
3838
properties: {},
3939
geometry: {

src/lib/id.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {customAlphabet} from 'nanoid/non-secure';
2+
3+
const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 32);
4+
5+
export function generateID() {
6+
return nanoid();
7+
}

test/api.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ afterEach(() => {
3333
deleteSpy = null;
3434
});
3535

36+
test('Draw.add', async (t) => {
37+
await t.test('should generate unique ID', () => {
38+
const [id] = Draw.add(getGeoJSON('point'));
39+
40+
assert.equal(typeof id, 'string', 'valid string id returned on add');
41+
assert.equal(id.length, 32, 'valid string id length');
42+
});
43+
});
44+
3645
test('Draw.getFeatureIdsAt', async () => {
3746
const feature = getGeoJSON('point');
3847
const [id] = Draw.add(feature);

test/utils/create_feature.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import hat from 'hat';
1+
import {generateID} from '../../src/lib/id.js';
22
import getGeoJSON from './get_geojson.js';
33

4-
const hatRack = hat.rack();
4+
const usedIds = new Set();
5+
6+
export function generateUniqueID() {
7+
let id = generateID();
8+
while (usedIds.has(id)) {
9+
id = generateID();
10+
}
11+
usedIds.add(id);
12+
return id;
13+
}
514

615
export default function createFeature(featureType) {
716
const feature = Object.assign({
8-
id: hatRack(),
17+
id: generateUniqueID(),
918
properties: {}
1019
}, getGeoJSON(featureType));
1120
feature.toGeoJSON = () => feature;

0 commit comments

Comments
 (0)