Skip to content

Commit ea4e64d

Browse files
missinglinkorangejulius
authored andcommitted
refactor addParent() and removeParent()
1 parent e9deed2 commit ea4e64d

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

Document.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,40 @@ Document.prototype.hasName = model.hasChild( 'name' );
120120
Document.prototype.delName = model.delChild( 'name' );
121121

122122
// parent
123-
Document.prototype.addParent = model.pushChild( 'parent' )
124-
.validate( valid.type('string') )
125-
.validate( valid.truthy() );
123+
Document.prototype.addParent = function( field, name, id, abbr ){
124+
var add = model.pushChild( 'parent' )
125+
.validate( valid.type('string') )
126+
.validate( valid.truthy() )
127+
.bind(this);
128+
129+
// mandatory fields, eg: 'country', 'country_id'
130+
add( field, name );
131+
add( field + '_id', id );
132+
133+
// optional field, eg: 'country_abbr'
134+
if( arguments.length > 3 ){
135+
add( field + '_abbr', abbr );
136+
}
137+
138+
// chainable
139+
return this;
140+
};
141+
142+
Document.prototype.removeParent = function( field, name, id, abbr ){
143+
var rm = model.spliceChild( 'parent' ).bind(this);
144+
145+
// mandatory fields, eg: 'country', 'country_id'
146+
rm( field, name );
147+
rm( field + '_id', id );
126148

127-
Document.prototype.removeParent = model.spliceChild( 'parent' );
149+
// optional field, eg: 'country_abbr'
150+
if( arguments.length > 3 ){
151+
rm( field + '_abbr', abbr );
152+
}
153+
154+
// chainable
155+
return this;
156+
};
128157

129158
// address
130159
Document.prototype.setAddress = function ( prop, val ){
@@ -261,7 +290,7 @@ Document.adminFields = ['admin0','admin1','admin1_abbr','admin2','local_admin','
261290

262291
Document.addressFields = ['name', 'number', 'street', 'zip'];
263292

264-
Document.parentFields = ['alpha3','country','country_abbr','country_id','region','region_abbr',
293+
Document.parentFields = ['country','country_abbr','country_id','region','region_abbr',
265294
'region_id','county','county_abbr','county_id','locality','locality_abbr','locality_id',
266295
'localadmin','localadmin_abbr','localadmin_id','neighbourhood','neighbourhood_abbr','neighbourhood_id'];
267296

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ var poi = new Document( 'geoname', 'venue', 1003 )
2222
.setName( 'alt', 'Haggerston City Farm' )
2323
.setAdmin( 'admin0', 'Great Britain' )
2424
.setAdmin( 'neighborhood', 'Shoreditch' )
25-
.addParent( 'country', 'Great Britain' )
26-
.addParent( 'neighbourhood', 'Shoreditch' )
27-
.addParent( 'alpha3', 'GBR' )
28-
.removeParent( 'alpha3', 'GBR' )
25+
.addParent( 'country', 'Great Britain', '1001', 'GreatB' )
26+
.addParent( 'neighbourhood', 'Shoreditch', '2002' )
2927
.setAddress( 'number', '10' )
3028
.setAddress( 'street', 'pelias place' )
3129
.addCategory( 'foo' )

errors.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
var util = require('util');
3+
24
/**
35
Custom Errors used in order to distinguish them from generic Errors:
46
@@ -21,13 +23,13 @@
2123

2224
// Custom Error - PeliasModelError
2325
function PeliasModelError(message) {
24-
this.name = 'PeliasModelError';
25-
this.message = message;
26-
this.stack = (new Error()).stack;
26+
Error.captureStackTrace(this, this.constructor);
27+
this.name = 'PeliasModelError';
28+
this.message = message;
2729
}
2830

2931
// Extends from js Error object
30-
PeliasModelError.prototype = new Error();
32+
util.inherits(PeliasModelError, Error);
3133

3234
// export
3335
module.exports.PeliasModelError = PeliasModelError;

test/document/parent.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ module.exports.tests = {};
66
module.exports.tests.addParent = function(test) {
77
test('addParent', function(t) {
88
var doc = new Document('mysource','mylayer','myid');
9-
t.equal(doc.addParent('country','liberland'), doc, 'chainable');
9+
t.equal(doc.addParent('country','liberland', 'liber_id', 'liber_abbr'), doc, 'chainable');
1010
t.equal(doc.parent.country[0], 'liberland', 'adder works');
11+
t.equal(doc.parent.country_id[0], 'liber_id', 'adder works');
12+
t.equal(doc.parent.country_abbr[0], 'liber_abbr', 'adder works');
13+
t.end();
14+
});
15+
test('addParent - omit abbr', function(t) {
16+
var doc = new Document('mysource','mylayer','myid');
17+
t.equal(doc.addParent('country','liberland', 'liber_id'), doc, 'chainable');
18+
t.equal(doc.parent.country[0], 'liberland', 'adder works');
19+
t.equal(doc.parent.country_id[0], 'liber_id', 'adder works');
1120
t.end();
1221
});
1322
test('addParent - validate', function(t) {
@@ -21,8 +30,8 @@ module.exports.tests.addParent = function(test) {
2130
t.throws( doc.addParent.bind(doc, 'country', null), null, 'invalid property' );
2231
t.throws( doc.addParent.bind(doc, 'country', '\n'), null, 'invalid property' );
2332
t.equal(doc.parent.street, undefined, 'property unchanged');
24-
t.doesNotThrow( doc.addParent.bind(doc, 'country', 'foo'), null, 'invalid property' );
25-
t.doesNotThrow( doc.addParent.bind(doc, 'country', '1'), null, 'invalid property' );
33+
t.doesNotThrow( doc.addParent.bind(doc, 'country', 'foo', 'bar','baz'), null, 'valid property' );
34+
t.doesNotThrow( doc.addParent.bind(doc, 'country', '1', '1', '1'), null, 'valid property' );
2635
t.end();
2736
});
2837
};
@@ -31,8 +40,23 @@ module.exports.tests.removeParent = function(test) {
3140
test('removeParent', function(t) {
3241
var doc = new Document('mysource','mylayer','myid');
3342
doc.parent.country[0] = 'liberland';
34-
t.equal(doc.removeParent('country','liberland'), doc, 'chainable');
43+
doc.parent.country_id[0] = 'liber_id';
44+
doc.parent.country_abbr[0] = 'liber_abbr';
45+
t.equal(doc.removeParent('country','liberland', 'liber_id', 'liber_abbr'), doc, 'chainable');
46+
t.equal(doc.parent.country.length, 0, 'remover works');
47+
t.equal(doc.parent.country_id.length, 0, 'remover works');
48+
t.equal(doc.parent.country_abbr.length, 0, 'remover works');
49+
t.end();
50+
});
51+
test('removeParent - omit abbr', function(t) {
52+
var doc = new Document('mysource','mylayer','myid');
53+
doc.parent.country[0] = 'liberland';
54+
doc.parent.country_id[0] = 'liber_id';
55+
doc.parent.country_abbr[0] = 'liber_abbr';
56+
t.equal(doc.removeParent('country','liberland', 'liber_id'), doc, 'chainable');
3557
t.equal(doc.parent.country.length, 0, 'remover works');
58+
t.equal(doc.parent.country_id.length, 0, 'remover works');
59+
t.equal(doc.parent.country_abbr.length, 1, 'abbr ignored');
3660
t.end();
3761
});
3862
test('removeParent - validate', function(t) {

test/serialize/test.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module.exports.tests.minimal = function(test) {
3030
'name': {},
3131
'phrase': {},
3232
'parent': {
33-
'alpha3': [],
3433
'country': [],
3534
'country_abbr': [],
3635
'country_id': [],
@@ -71,10 +70,8 @@ module.exports.tests.complete = function(test) {
7170
.setName( 'alt', 'Haggerston City Farm' )
7271
.setAdmin( 'admin0', 'Great Britain' )
7372
.setAdmin( 'neighborhood', 'Shoreditch' )
74-
.addParent( 'country', 'Great Britain' )
75-
.addParent( 'neighbourhood', 'Shoreditch' )
76-
.addParent( 'alpha3', 'GBR' )
77-
.removeParent( 'alpha3', 'GBR' )
73+
.addParent( 'country', 'Great Britain', '1001', 'GreatB' )
74+
.addParent( 'neighbourhood', 'Shoreditch', '2002' )
7875
.setAddress( 'number', '10' )
7976
.setAddress( 'street', 'pelias place' )
8077
.addCategory( 'foo' )
@@ -98,6 +95,7 @@ module.exports.tests.complete = function(test) {
9895
// data partitioning
9996
'source': 'geoname',
10097
'layer': 'venue',
98+
'alpha3': 'GBR',
10199

102100
// place name (ngram analysis)
103101
'name':{
@@ -118,16 +116,14 @@ module.exports.tests.complete = function(test) {
118116
},
119117

120118
// Quattroshapes fields
121-
'alpha3': 'GBR',
122119
'admin0': 'Great Britain',
123120
'neighborhood': 'Shoreditch',
124121

125122
// WOF fields
126123
'parent': {
127-
'alpha3': [],
128124
'country': ['Great Britain'],
129-
'country_abbr': [],
130-
'country_id': [],
125+
'country_abbr': ['GreatB'],
126+
'country_id': ['1001'],
131127
'county': [],
132128
'county_abbr': [],
133129
'county_id': [],
@@ -139,7 +135,7 @@ module.exports.tests.complete = function(test) {
139135
'locality_id': [],
140136
'neighbourhood': ['Shoreditch'],
141137
'neighbourhood_abbr': [],
142-
'neighbourhood_id': [],
138+
'neighbourhood_id': ['2002'],
143139
'region': [],
144140
'region_abbr': [],
145141
'region_id': []

0 commit comments

Comments
 (0)