Skip to content

Commit

Permalink
sug-699 add fias params
Browse files Browse the repository at this point in the history
  • Loading branch information
format1981 committed Nov 25, 2016
1 parent 346c623 commit 660aaf9
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

### 25.11.2016 16.10.2

* добавлена поддержка ограничения по коду ФИАС

### 22.09.2016 16.8.8

* исправлены ограничения для компаний
Expand Down
100 changes: 96 additions & 4 deletions dist/js/jquery.suggestions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* DaData.ru Suggestions jQuery plugin, version 16.10.1
* DaData.ru Suggestions jQuery plugin, version 16.10.2
*
* DaData.ru Suggestions jQuery plugin is freely distributable under the terms of MIT-style license
* Built on DevBridge Autocomplete for jQuery (https://github.com/devbridge/jQuery-Autocomplete)
Expand Down Expand Up @@ -201,6 +201,25 @@
})
}) : array1;
},
/**
* Пересечение массивов: ([1,2,3,4], [2,4,5,6]) => [2,4]
* Исходные массивы не меняются
* @param {Array} array1
* @param {Array} array2
* @returns {Array}
*/
arraysIntersection: function(array1, array2) {
var result = [];
if (!$.isArray(array1) || !$.isArray(array2)) {
return result;
}
$.each(array1, function(index, item) {
if ($.inArray(item, array2) >= 0) {
result.push(item);
}
});
return result;
},
getWords: function(str, stopwords) {
// Split numbers and letters written together
str = str.replace(/(\d+)([а-яА-ЯёЁ]{2,})/g, '$1 $2')
Expand Down Expand Up @@ -262,7 +281,25 @@
});

return result;
},

/**
* Возвращает массив с ключами переданного объекта
* Используется нативный Object.keys если он есть
* @param {Object} obj
* @returns {Array}
*/
objectKeys: function(obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
$.each(obj, function(name) {
keys.push(name);
});
return keys;
}

};
}());

Expand Down Expand Up @@ -533,6 +570,42 @@
forBounds: true,
forLocations: false,
kladrFormat: { digits: 19 }
},
{
id: 'region_fias_id',
fields: ['region_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'area_fias_id',
fields: ['area_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'city_fias_id',
fields: ['city_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'city_district_fias_id',
fields: ['city_district_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'settlement_fias_id',
fields: ['settlement_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'street_fias_id',
fields: ['street_fias_id'],
forBounds: false,
forLocations: true
}
];

Expand Down Expand Up @@ -911,7 +984,7 @@

Suggestions.defaultOptions = defaultOptions;

Suggestions.version = '16.10.1';
Suggestions.version = '16.10.2';

$.Suggestions = Suggestions;

Expand Down Expand Up @@ -2923,6 +2996,15 @@
restrict_value: false
};

var fiasParamNames = [
'region_fias_id',
'area_fias_id',
'city_fias_id',
'city_district_fias_id',
'settlement_fias_id',
'street_fias_id'
];

/**
* Compares two suggestion objects
* @param suggestion
Expand All @@ -2946,7 +3028,10 @@
* @constructor
*/
var ConstraintLocation = function(data, instance){
var that = this;
var that = this,
fieldNames,
fiasFieldNames,
fiasFields = {};

that.instance = instance;
that.fields = {};
Expand All @@ -2963,7 +3048,14 @@
});
}

if (that.fields.kladr_id) {
fieldNames = utils.objectKeys(that.fields);
fiasFieldNames = utils.arraysIntersection(fieldNames, fiasParamNames);
if (fiasFieldNames.length) {
$.each(fiasFieldNames, function(index, fieldName) {
fiasFields[fieldName] = that.fields[fieldName];
});
that.fields = fiasFields;
} else if (that.fields.kladr_id) {
that.fields = { kladr_id: that.fields.kladr_id };
that.specificity = that.getKladrSpecificity(that.fields.kladr_id);
}
Expand Down
6 changes: 3 additions & 3 deletions dist/js/jquery.suggestions.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suggestions-jquery",
"version": "16.10.1",
"version": "16.10.2",
"description": "DaData.ru Suggestions jQuery plugin",
"homepage": "https://github.com/hflabs/suggestions-jquery",
"author": {
Expand Down
23 changes: 21 additions & 2 deletions src/includes/constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
restrict_value: false
};

var fiasParamNames = [
'region_fias_id',
'area_fias_id',
'city_fias_id',
'city_district_fias_id',
'settlement_fias_id',
'street_fias_id'
];

/**
* Compares two suggestion objects
* @param suggestion
Expand All @@ -30,7 +39,10 @@
* @constructor
*/
var ConstraintLocation = function(data, instance){
var that = this;
var that = this,
fieldNames,
fiasFieldNames,
fiasFields = {};

that.instance = instance;
that.fields = {};
Expand All @@ -47,7 +59,14 @@
});
}

if (that.fields.kladr_id) {
fieldNames = utils.objectKeys(that.fields);
fiasFieldNames = utils.arraysIntersection(fieldNames, fiasParamNames);
if (fiasFieldNames.length) {
$.each(fiasFieldNames, function(index, fieldName) {
fiasFields[fieldName] = that.fields[fieldName];
});
that.fields = fiasFields;
} else if (that.fields.kladr_id) {
that.fields = { kladr_id: that.fields.kladr_id };
that.specificity = that.getKladrSpecificity(that.fields.kladr_id);
}
Expand Down
36 changes: 36 additions & 0 deletions src/includes/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@
forBounds: true,
forLocations: false,
kladrFormat: { digits: 19 }
},
{
id: 'region_fias_id',
fields: ['region_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'area_fias_id',
fields: ['area_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'city_fias_id',
fields: ['city_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'city_district_fias_id',
fields: ['city_district_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'settlement_fias_id',
fields: ['settlement_fias_id'],
forBounds: false,
forLocations: true
},
{
id: 'street_fias_id',
fields: ['street_fias_id'],
forBounds: false,
forLocations: true
}
];

Expand Down
37 changes: 37 additions & 0 deletions src/includes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@
})
}) : array1;
},
/**
* Пересечение массивов: ([1,2,3,4], [2,4,5,6]) => [2,4]
* Исходные массивы не меняются
* @param {Array} array1
* @param {Array} array2
* @returns {Array}
*/
arraysIntersection: function(array1, array2) {
var result = [];
if (!$.isArray(array1) || !$.isArray(array2)) {
return result;
}
$.each(array1, function(index, item) {
if ($.inArray(item, array2) >= 0) {
result.push(item);
}
});
return result;
},
getWords: function(str, stopwords) {
// Split numbers and letters written together
str = str.replace(/(\d+)([а-яА-ЯёЁ]{2,})/g, '$1 $2')
Expand Down Expand Up @@ -176,6 +195,24 @@
});

return result;
},

/**
* Возвращает массив с ключами переданного объекта
* Используется нативный Object.keys если он есть
* @param {Object} obj
* @returns {Array}
*/
objectKeys: function(obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
$.each(obj, function(name) {
keys.push(name);
});
return keys;
}

};
}());
12 changes: 12 additions & 0 deletions test/specs/constraint_location_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe('Constraint Location', function () {
expect(Object.keys(location.getFields())).toEqual(['kladr_id']);
});

// наличие фиас параметров если переданы
it('should use fias params if specified', function () {
var location = new $.Suggestions.ConstraintLocation({
'city': 'Тольятти',
'kladr_id': '6300000700000',
'city_fias_id': '1000000',
'street_fias_id': '1000000'
}, this.instance);

expect(Object.keys(location.getFields())).toEqual(['city_fias_id', 'street_fias_id']);
});

it('should determine specificity', function () {
var location = new $.Suggestions.ConstraintLocation({
'city': 'Тольятти'
Expand Down
Loading

0 comments on commit 660aaf9

Please sign in to comment.