-
Notifications
You must be signed in to change notification settings - Fork 25
/
id-generator.ts
77 lines (69 loc) · 2.48 KB
/
id-generator.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Md5 } from 'ts-md5';
import { uuid } from '@igo2/utils';
import { AnyDataSourceOptions } from '../shared/datasources/any-datasource.interface';
import { DataSourceOptions } from '../shared/datasources/datasource.interface';
import { WMSDataSourceOptions } from '../shared/datasources/wms-datasource.interface';
import { WMTSDataSourceOptions } from '../shared/datasources/wmts-datasource.interface';
/**
* Generate a id from it's datasource options.
* @param options Data source options
* @returns A id
*/
export function generateIdFromSourceOptions(options: DataSourceOptions): string {
const generators = {
wms: generateWMSIdFromSourceOptions,
wmts: generateWMTSIdFromSourceOptions,
xyz: generateXYZIdFromSourceOptions,
feature: generateFeatureIdFromSourceOptions,
osm: (_options: AnyDataSourceOptions) => 'OSM'
};
const generator = generators[options.type] || generateId;
return generator(options);
}
/**
* Generate a id from WMS data source options
* @param options WMS data source options
* @returns A md5 hash of the the url and layers
*/
export function generateWMSIdFromSourceOptions(options: WMSDataSourceOptions) {
const layers = options.params.LAYERS;
const url = options.url.charAt(0) === '/' ? window.location.origin + options.url : options.url;
const chain = 'wms' + url + layers;
return Md5.hashStr(chain) as string;
}
/**
* Generate a id from WMTS data source options
* @param options WMTS data source options
* @returns A md5 hash of the the url and layer
*/
export function generateWMTSIdFromSourceOptions(options: WMTSDataSourceOptions) {
const layer = options.layer;
const chain = 'wmts' + options.url + layer;
return Md5.hashStr(chain) as string;
}
/**
* Generate a id from XYZ data source options
* @param options XYZ data source options
* @returns A md5 hash of the the url and layer
*/
export function generateXYZIdFromSourceOptions(options: WMTSDataSourceOptions) {
const chain = 'xyz' + options.url;
return Md5.hashStr(chain) as string;
}
/**
* Generate a id from feature data source options
* @param options XYZ data source options
* @returns A md5 hash of the the url and layer
*/
export function generateFeatureIdFromSourceOptions(options: WMTSDataSourceOptions) {
if (! options.url) { return generateId(options); }
const chain = 'feature' + options.url;
return Md5.hashStr(chain) as string;
}
/**
* Generate a unique id
* @returns A uuid
*/
export function generateId(options: AnyDataSourceOptions) {
return uuid();
}