🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

jquery-postcodes

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jquery-postcodes - npm Package Compare versions

Comparing version

to
1.1.1

examples/bootstrap-theme.css

83

dist/postcodes.js

@@ -1,7 +0,6 @@

/*! Ideal Postcodes jQuery Plugin - v0.1.1 - 2013-09-20
/*! Ideal Postcodes jQuery Plugin - v1.1.1 - 2014-03-20
* https://github.com/ideal-postcodes/jquery.postcodes
* Copyright (c) 2013 Christopher Blanchard; Licensed MIT */
* Copyright (c) 2014 Christopher Blanchard; Licensed MIT */
(function($) {
"use strict";
var defaults = {

@@ -39,7 +38,11 @@ // Please Enter your API Key

// Below is not required
/*
* Below is not required
*
*/
api_endpoint: "https://api.ideal-postcodes.co.uk/v1",
// Input Field Configuration
$input: "",
$input: undefined,
input_label: "Please enter your postcode",

@@ -51,3 +54,3 @@ input_muted_style: "color:#CBCBCB;",

// Button configuration
$button: "",
$button: undefined,
button_id: "idpc_button",

@@ -58,2 +61,3 @@ button_label: "Find my Address",

// Dropdown configuration
$dropdown: undefined,
dropdown_id: "idpc_dropdown",

@@ -64,3 +68,3 @@ dropdown_select_message: "Please select your address",

// Error Message Configuration
$error_message: "",
$error_message: undefined,
error_message_id: "idpc_error_message",

@@ -77,3 +81,8 @@ error_message_invalid_postcode: "Please check your postcode, it seems to be incorrect",

// Debug - Set to true to pipe API error messages to client
debug_mode: false
debug_mode: false,
// Register callbacks at specific stages
onLookupSuccess: undefined,
onLookupError: undefined,
onAddressSelected: undefined
};

@@ -103,3 +112,3 @@

// Create and append postcode input and submit button to specified div context
setup_dropdown: function (context, options) {
setupPostcodeInput: function (context, options) {
Idpc.$context = context;

@@ -132,2 +141,7 @@

})
.keypress(function (event) {
if (event.which === 13) {
Idpc.$button.trigger("click");
}
})
.appendTo(Idpc.$context);

@@ -149,2 +163,3 @@

if (Idpc.last_lookup !== postcode) {
Idpc.last_lookup = postcode;
Idpc.disable_lookup_button();

@@ -159,14 +174,16 @@ Idpc.clear_existing_fields();

// Perform AJAX (JSONP) request
// Request postcode via JSONP
lookupPostcode: function (postcode) {
if (Idpc.valid_postcode(postcode)) {
if ($.idealPostcodes.validatePostcodeFormat(postcode)) {
var success = function (data) {
Idpc.handle_api_success(data);
$.event.trigger("completedJsonp"); // added for API testing, better solution needed
// To introduce callback
if (Idpc.onLookupSuccess) {
Idpc.onLookupSuccess(data);
}
};
var error = function () {
Idpc.show_error("Unable to connect to server");
$.event.trigger("completedJsonp");
// To introduce callback
if (Idpc.onLookupError) {
Idpc.onLookupError();
}
};

@@ -191,8 +208,2 @@ $.idealPostcodes.lookupPostcode(postcode, Idpc.api_key, success, error);

// Test for valid postcode format
valid_postcode: function (postcode) {
var regex = /^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/;
return !!postcode.match(regex);
},
// Callback if JSONP request returns with code 2000

@@ -277,3 +288,3 @@ handle_api_success: function (data) {

var data = Idpc.result;
return $address_dropdown.change(function () {
$address_dropdown.change(function () {
var index = $(this).val();

@@ -283,3 +294,7 @@ if (index >= 0) {

}
if (Idpc.onAddressSelected) {
Idpc.onAddressSelected.call(this, data[index]);
}
});
return $address_dropdown;
},

@@ -306,2 +321,3 @@

$.idealPostcodes = {
// Expost defaults for testing

@@ -317,2 +333,6 @@ defaults: function () {

validatePostcodeFormat: function (postcode) {
return !!postcode.match(/^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/);
},
// Lookup postcode on API

@@ -339,2 +359,19 @@ lookupPostcode: function (postcode, api_key, success, error) {

},
clearAll: function () {
Idpc.$context = null;
if (Idpc.$input) {
Idpc.$input.remove();
}
if (Idpc.$button) {
Idpc.$button.remove();
}
if (Idpc.$dropdown) {
Idpc.$dropdown.remove();
}
if (Idpc.$error_message) {
Idpc.$error_message.remove();
}
}
};

@@ -344,3 +381,3 @@

$.fn.setupPostcodeLookup = function (options) {
Idpc.setup_dropdown($(this), options);
Idpc.setupPostcodeInput($(this), options);
return this;

@@ -347,0 +384,0 @@ };

@@ -1,4 +0,4 @@

/*! Ideal Postcodes jQuery Plugin - v0.1.1 - 2013-09-20
/*! Ideal Postcodes jQuery Plugin - v1.1.1 - 2014-03-20
* https://github.com/ideal-postcodes/jquery.postcodes
* Copyright (c) 2013 Christopher Blanchard; Licensed MIT */
!function(a){"use strict";var b={api_key:"",output_fields:{line_1:"#line_1",line_2:"#line_2",line_3:"#line_3",post_town:"#post_town",postcode:"#postcode",postcode_inward:void 0,postcode_outward:void 0,udprn:void 0,dependant_locality:void 0,double_dependant_locality:void 0,thoroughfare:void 0,dependant_thoroughfare:void 0,building_number:void 0,building_name:void 0,sub_building_name:void 0,po_box:void 0,department_name:void 0,organisation_name:void 0,postcode_type:void 0,su_organisation_indicator:void 0,delivery_point_suffix:void 0},api_endpoint:"https://api.ideal-postcodes.co.uk/v1",$input:"",input_label:"Please enter your postcode",input_muted_style:"color:#CBCBCB;",input_class:"",input_id:"idpc_input",$button:"",button_id:"idpc_button",button_label:"Find my Address",button_class:"",dropdown_id:"idpc_dropdown",dropdown_select_message:"Please select your address",dropdown_class:"",$error_message:"",error_message_id:"idpc_error_message",error_message_invalid_postcode:"Please check your postcode, it seems to be incorrect",error_message_not_found:"Your postcode could not be found. Please type in your address",error_message_default:"Sorry, we weren't able to get the address you were looking for. Please type your address manually",error_message_class:"",last_lookup:"",disable_interval:1e3,debug_mode:!1},c={init:function(d){a.extend(c,b),d&&a.extend(c,d),c.rig_output_fields()},rig_output_fields:function(){var b={};for(var d in c.output_fields)void 0!==c.output_fields[d]&&(b[d]=a(c.output_fields[d]));c.output_fields=b},setup_dropdown:function(b,d){c.$context=b,d&&a.extend(c,d),c.$input=a("<input />",{type:"text",id:c.input_id,value:c.input_label,"class":c.input_class}).val(c.input_label).attr("style",c.input_muted_style).focus(function(){c.$input.removeAttr("style").val("")}).blur(function(){c.$input.val()||(c.$input.val(c.input_label),c.$input.attr("style",c.input_muted_style))}).submit(function(){return!1}).appendTo(c.$context),c.$button=a("<button />",{html:c.button_label,id:c.button_id,"class":c.button_class}).attr("type","button").attr("onclick","return false;").submit(function(){return!1}).click(function(){var a=c.$input.val();return c.last_lookup!==a&&(c.disable_lookup_button(),c.clear_existing_fields(),c.lookupPostcode(a)),!1}).appendTo(c.$context)},lookupPostcode:function(b){if(c.valid_postcode(b)){var d=function(b){c.handle_api_success(b),a.event.trigger("completedJsonp")},e=function(){c.show_error("Unable to connect to server"),a.event.trigger("completedJsonp")};a.idealPostcodes.lookupPostcode(b,c.api_key,d,e)}else c.show_error(c.error_message_invalid_postcode)},disable_lookup_button:function(a){c.$button.prop("disabled",!0).html(a||"Looking up postcode...")},enable_lookup_button:function(){setTimeout(function(){c.$button.prop("disabled",!1).html(c.button_label)},c.disable_interval)},valid_postcode:function(a){var b=/^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/;return!!a.match(b)},handle_api_success:function(a){c.response_code=a.code,c.response_message=a.message,c.result=a.result,2e3===c.response_code?(c.last_lookup=c.$input.val(),c.show_dropdown(c.result).appendTo(c.$context),c.enable_lookup_button()):c.handle_api_error()},handle_api_error:function(){4040===c.response_code?c.show_error(c.error_message_not_found):c.debug_mode?c.show_error("("+c.response_code+") "+c.response_message):c.show_error(c.error_message_default),c.enable_lookup_button()},clear_existing_fields:function(){c.clear_dropdown_menu(),c.clear_error_messages(),c.clear_input_fields()},clear_dropdown_menu:function(){c.$dropdown&&c.$dropdown.length&&c.$dropdown.remove()},clear_error_messages:function(){c.$error_message&&c.$error_message.length&&c.$error_message.remove()},clear_input_fields:function(){for(var a in c.output_fields)c.output_fields[a].val("")},show_dropdown:function(b){var d=b.length,e=a("<select />",{id:c.dropdown_id,"class":c.dropdown_class});a("<option />",{value:"ideal",text:c.dropdown_select_message}).appendTo(e);for(var f=0;d>f;f+=1)a("<option />",{value:f,text:b[f].line_1+" "+b[f].line_2}).appendTo(e);return c.link_to_fields(e),c.$dropdown=e,e},link_to_fields:function(b){var d=c.result;return b.change(function(){var b=a(this).val();b>=0&&c.populate_output_fields(d[b])})},populate_output_fields:function(a){for(var b in c.output_fields)c.output_fields[b].val(a[b])},show_error:function(b){c.enable_lookup_button(),c.$error_message=a("<p />",{html:b,id:c.error_message_id,"class":c.error_message_class}).appendTo(c.$context)}};a.idealPostcodes={defaults:function(){return b},setup:function(a){c.init(a)},lookupPostcode:function(d,e,f,g){var h=c.api_endpoint||b.api_endpoint,i="postcodes",j=[h,i,d].join("/"),k={url:j,data:{api_key:e},dataType:"jsonp",timeout:5e3,success:f};g&&(k.error=g),a.ajax(k)}},a.fn.setupPostcodeLookup=function(b){return c.setup_dropdown(a(this),b),this}}(jQuery);
* Copyright (c) 2014 Christopher Blanchard; Licensed MIT */
!function(a){"use strict";var b={api_key:"",output_fields:{line_1:"#line_1",line_2:"#line_2",line_3:"#line_3",post_town:"#post_town",postcode:"#postcode",postcode_inward:void 0,postcode_outward:void 0,udprn:void 0,dependant_locality:void 0,double_dependant_locality:void 0,thoroughfare:void 0,dependant_thoroughfare:void 0,building_number:void 0,building_name:void 0,sub_building_name:void 0,po_box:void 0,department_name:void 0,organisation_name:void 0,postcode_type:void 0,su_organisation_indicator:void 0,delivery_point_suffix:void 0},api_endpoint:"https://api.ideal-postcodes.co.uk/v1",$input:void 0,input_label:"Please enter your postcode",input_muted_style:"color:#CBCBCB;",input_class:"",input_id:"idpc_input",$button:void 0,button_id:"idpc_button",button_label:"Find my Address",button_class:"",$dropdown:void 0,dropdown_id:"idpc_dropdown",dropdown_select_message:"Please select your address",dropdown_class:"",$error_message:void 0,error_message_id:"idpc_error_message",error_message_invalid_postcode:"Please check your postcode, it seems to be incorrect",error_message_not_found:"Your postcode could not be found. Please type in your address",error_message_default:"Sorry, we weren't able to get the address you were looking for. Please type your address manually",error_message_class:"",last_lookup:"",disable_interval:1e3,debug_mode:!1,onLookupSuccess:void 0,onLookupError:void 0,onAddressSelected:void 0},c={init:function(d){a.extend(c,b),d&&a.extend(c,d),c.rig_output_fields()},rig_output_fields:function(){var b={};for(var d in c.output_fields)void 0!==c.output_fields[d]&&(b[d]=a(c.output_fields[d]));c.output_fields=b},setupPostcodeInput:function(b,d){c.$context=b,d&&a.extend(c,d),c.$input=a("<input />",{type:"text",id:c.input_id,value:c.input_label,"class":c.input_class}).val(c.input_label).attr("style",c.input_muted_style).focus(function(){c.$input.removeAttr("style").val("")}).blur(function(){c.$input.val()||(c.$input.val(c.input_label),c.$input.attr("style",c.input_muted_style))}).submit(function(){return!1}).keypress(function(a){13===a.which&&c.$button.trigger("click")}).appendTo(c.$context),c.$button=a("<button />",{html:c.button_label,id:c.button_id,"class":c.button_class}).attr("type","button").attr("onclick","return false;").submit(function(){return!1}).click(function(){var a=c.$input.val();return c.last_lookup!==a&&(c.last_lookup=a,c.disable_lookup_button(),c.clear_existing_fields(),c.lookupPostcode(a)),!1}).appendTo(c.$context)},lookupPostcode:function(b){if(a.idealPostcodes.validatePostcodeFormat(b)){var d=function(a){c.handle_api_success(a),c.onLookupSuccess&&c.onLookupSuccess(a)},e=function(){c.show_error("Unable to connect to server"),c.onLookupError&&c.onLookupError()};a.idealPostcodes.lookupPostcode(b,c.api_key,d,e)}else c.show_error(c.error_message_invalid_postcode)},disable_lookup_button:function(a){c.$button.prop("disabled",!0).html(a||"Looking up postcode...")},enable_lookup_button:function(){setTimeout(function(){c.$button.prop("disabled",!1).html(c.button_label)},c.disable_interval)},handle_api_success:function(a){c.response_code=a.code,c.response_message=a.message,c.result=a.result,2e3===c.response_code?(c.last_lookup=c.$input.val(),c.show_dropdown(c.result).appendTo(c.$context),c.enable_lookup_button()):c.handle_api_error()},handle_api_error:function(){4040===c.response_code?c.show_error(c.error_message_not_found):c.debug_mode?c.show_error("("+c.response_code+") "+c.response_message):c.show_error(c.error_message_default),c.enable_lookup_button()},clear_existing_fields:function(){c.clear_dropdown_menu(),c.clear_error_messages(),c.clear_input_fields()},clear_dropdown_menu:function(){c.$dropdown&&c.$dropdown.length&&c.$dropdown.remove()},clear_error_messages:function(){c.$error_message&&c.$error_message.length&&c.$error_message.remove()},clear_input_fields:function(){for(var a in c.output_fields)c.output_fields[a].val("")},show_dropdown:function(b){var d=b.length,e=a("<select />",{id:c.dropdown_id,"class":c.dropdown_class});a("<option />",{value:"ideal",text:c.dropdown_select_message}).appendTo(e);for(var f=0;d>f;f+=1)a("<option />",{value:f,text:b[f].line_1+" "+b[f].line_2}).appendTo(e);return c.link_to_fields(e),c.$dropdown=e,e},link_to_fields:function(b){var d=c.result;return b.change(function(){var b=a(this).val();b>=0&&c.populate_output_fields(d[b]),c.onAddressSelected&&c.onAddressSelected.call(this,d[b])}),b},populate_output_fields:function(a){for(var b in c.output_fields)c.output_fields[b].val(a[b])},show_error:function(b){c.enable_lookup_button(),c.$error_message=a("<p />",{html:b,id:c.error_message_id,"class":c.error_message_class}).appendTo(c.$context)}};a.idealPostcodes={defaults:function(){return b},setup:function(a){c.init(a)},validatePostcodeFormat:function(a){return!!a.match(/^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/)},lookupPostcode:function(d,e,f,g){var h=c.api_endpoint||b.api_endpoint,i="postcodes",j=[h,i,d].join("/"),k={url:j,data:{api_key:e},dataType:"jsonp",timeout:5e3,success:f};g&&(k.error=g),a.ajax(k)},clearAll:function(){c.$context=null,c.$input&&c.$input.remove(),c.$button&&c.$button.remove(),c.$dropdown&&c.$dropdown.remove(),c.$error_message&&c.$error_message.remove()}},a.fn.setupPostcodeLookup=function(b){return c.setupPostcodeInput(a(this),b),this}}(jQuery);
{
"name": "jquery-postcodes",
"version": "1.0.0",
"version": "1.1.1",
"engines": {

@@ -5,0 +5,0 @@ "node": ">= 0.8.0"

@@ -5,3 +5,3 @@ {

"description": "Add UK Address lookups using postcodes on any address form using Royal Mail's addressing database",
"version": "0.1.1",
"version": "1.1.1",
"homepage": "https://github.com/ideal-postcodes/jquery.postcodes",

@@ -8,0 +8,0 @@ "author": {

@@ -15,3 +15,3 @@ # Postcodes jQuery Plugin

The plugin provides addresses according to [Royal Mail's Addressing Guidelines](http://www.royalmail.com/personal/help-and-support/How-do-I-address-my-mail-correctly). I.e. Maximum of 3 address lines, a Post Town and Postcode. More address data is also available.
The plugin provides addresses according to [Royal Mail's Addressing Guidelines](http://www.royalmail.com/personal/help-and-support/How-do-I-address-my-mail-correctly). i.e. Maximum of 3 address lines, a Post Town and Postcode. More address data is also available.

@@ -21,3 +21,3 @@ ![Ideal Postcodes Plugin Example](https://raw.github.com/ideal-postcodes/jquery.postcodes/master/misc/ideal_postcodes_snippet.png)

## Getting Started
1) **[Download the plugin](https://raw.github.com/ideal-postcodes/jquery.postcodes/master/dist/postcodes.min.js)** and load the plugin on your page
1) **[Download the plugin](https://raw.github.com/ideal-postcodes/jquery.postcodes/master/dist/postcodes.min.js)** and add to your page

@@ -31,3 +31,3 @@ ```html

3) **Configure Ideal Postcodes** by telling it your API Key and which fields to pipe Address data using CSS selectors
3) **Configure Ideal Postcodes** by applying your API Key and identifying your address fields with CSS selectors

@@ -42,6 +42,6 @@ ```html

line_1: '#first_line',
line_2: '#second_line',
line_3: '#third_line',
post_town: '#post_town',
postcode: '#postcode'
line_2: '#second_line',
line_3: '#third_line',
post_town: '#post_town',
postcode: '#postcode'
}

@@ -52,3 +52,3 @@ });

4) **Setup a Postcode Search Field** by inserting an empty <div> and calling .setupPostcodeLookup()
4) **Setup a Postcode Search Field** by inserting an empty div tag and calling .setupPostcodeLookup()

@@ -64,3 +64,3 @@ ```html

## More Addressing Data Available
## Available Data

@@ -71,32 +71,36 @@ By rigging just 5 fields in the above example, you will have the necessary information you need (and in the correct formatting) to identify any household in the UK by mail.

The additional information (and handle's to access them) is listed here:
Here's the complete list of available data fields:
**Unique Address Identifier**
- Unique Delivery Point Reference Number (udprn)
```javascript
output_fields: {
line_1: "<css_selector>", // Address Line 1
line_2: "<css_selector>", // Address Line 2
line_3: "<css_selector>", // Address Line 3
post_town: "<css_selector>", // Post Town
postcode: "<css_selector>", // Postcode
udprn : "<css_selector>", // Unique Delivery Point Reference Number
organisation_name : "<css_selector>", // Organisation Name
department_name : "<css_selector>", // Department Name
po_box : "<css_selector>", // PO Box Number
postcode_inward : "<css_selector>", // Postcode Inward Code
postcode_outward : "<css_selector>", // Postcode Outward Code
building_number : "<css_selector>", // Building Number
building_name : "<css_selector>", // Building Name
sub_building_name : "<css_selector>", // Sub Building Name
thoroughfare : "<css_selector>", // Thoroughfare
dependant_thoroughfare : "<css_selector>", // Dependant Thoroughfare
dependant_locality : "<css_selector>", // Dependant Locality
double_dependant_locality : "<css_selector>", // Double Dependant Locality
postcode_type : "<css_selector>", // Postcode Type
su_organisation_indicator : "<css_selector>", // Organisation Type
delivery_point_suffix : "<css_selector>", // Delivery Point Suffix
longitude : "<css_selector>", // Organisation Type
latitude : "<css_selector>", // Organisation Type
northings : "<css_selector>", // Northings
eastings : "<css_selector>" // Eastings
}
```
**Organisational Information**
- Organisation Name (organisation_name)
- Department Name (department_name)
More information on what these fields mean can be found [here](https://ideal-postcodes.co.uk/documentation/paf-data)
**Further Addressing Information**
- PO Box Number (po_box)
- Postcode Inward Code (postcode_inward)
- Postcode Outward Code (postcode_outward)
- Building Number (building_number)
- Building Name (building_name)
- Sub Building Name (sub_building_name)
- Thoroughfare (thoroughfare)
- Dependant Thoroughfare (dependant_thoroughfare)
- Dependant Locality (dependant_locality)
- Double Dependant Locality (double_dependant_locality)
**Miscellaneous**
- Postcode Type (postcode_type)
- Organisation Type (su_organisation_indicator)
- Delivery Point Suffix (delivery_point_suffix)
More information on what these fields mean can be found [here](https://ideal-postcodes.co.uk/paf-data)
To add them into your form, simply include it in output_fields when initialising Ideal Postcodes. The example below demonstrates how the organisation name can be routed to the input with the id "organisation_field"

@@ -107,12 +111,10 @@

$.idealPostcodes.setup({
// Set your API key
api_key: 'ak_Iddqd8Idkfa7Idchoppers8',
// Pass in CSS selectors pointing to your input fields to pipe the results
output_fields: {
line_1: '#first_line',
line_2: '#second_line',
line_3: '#third_line',
post_town: '#post_town',
postcode: '#postcode',
organisation_name: '#organisation_field'
line_2: '#second_line',
line_3: '#third_line',
post_town: '#post_town',
postcode: '#postcode',
organisation_name: '#organisation_field'
}

@@ -123,25 +125,2 @@ });

## Advanced Usage
### $.idealPostcodes.lookupPostcode(postcode, api_key, success[, error])
Performs a postcode lookup on the Ideal Postcodes API
**Arguments:**
- **postcode** (string) The postcode to lookup (case insensitive)
- **api_key** (string) Key to access service
- **success** (function) Asynchronous handler when data is received. If data.code !== 2000, an error has occured
- **error** (function, optional) Asynchronous handler in case of request timeout
**Example:**
```javascript
var API_KEY = 'ak_Iddqd8Idkfa7Idchoppers8';
$.lookupPostcode('ID11QD', API_KEY, function (data) {
console.log(data.result[0]); // => {postcode: "ID1 1QD", post_town: "LONDON", line_1: "Kingsley Hall", line_2: "Powis Road", line_3: ""}
})
```
## Documentation

@@ -155,5 +134,3 @@ More documentation can be found [here](https://ideal-postcodes.co.uk/documentation)

**v 1.0.0**
- Refactored setup into 2 step process for more flexibility
- Expanded postcode lookup to include complete PAF data including UDPRN, Organisation Names, etc.
- Bug fixes
***v1.1.1***
- Callbacks for successful lookup, erroneous lookup, address selection added

@@ -14,3 +14,2 @@ /*

"use strict";
var defaults = {

@@ -48,7 +47,11 @@ // Please Enter your API Key

// Below is not required
/*
* Below is not required
*
*/
api_endpoint: "https://api.ideal-postcodes.co.uk/v1",
// Input Field Configuration
$input: "",
$input: undefined,
input_label: "Please enter your postcode",

@@ -60,3 +63,3 @@ input_muted_style: "color:#CBCBCB;",

// Button configuration
$button: "",
$button: undefined,
button_id: "idpc_button",

@@ -67,2 +70,3 @@ button_label: "Find my Address",

// Dropdown configuration
$dropdown: undefined,
dropdown_id: "idpc_dropdown",

@@ -73,3 +77,3 @@ dropdown_select_message: "Please select your address",

// Error Message Configuration
$error_message: "",
$error_message: undefined,
error_message_id: "idpc_error_message",

@@ -86,3 +90,8 @@ error_message_invalid_postcode: "Please check your postcode, it seems to be incorrect",

// Debug - Set to true to pipe API error messages to client
debug_mode: false
debug_mode: false,
// Register callbacks at specific stages
onLookupSuccess: undefined,
onLookupError: undefined,
onAddressSelected: undefined
};

@@ -112,3 +121,3 @@

// Create and append postcode input and submit button to specified div context
setup_dropdown: function (context, options) {
setupPostcodeInput: function (context, options) {
Idpc.$context = context;

@@ -141,2 +150,7 @@

})
.keypress(function (event) {
if (event.which === 13) {
Idpc.$button.trigger("click");
}
})
.appendTo(Idpc.$context);

@@ -158,2 +172,3 @@

if (Idpc.last_lookup !== postcode) {
Idpc.last_lookup = postcode;
Idpc.disable_lookup_button();

@@ -168,14 +183,16 @@ Idpc.clear_existing_fields();

// Perform AJAX (JSONP) request
// Request postcode via JSONP
lookupPostcode: function (postcode) {
if (Idpc.valid_postcode(postcode)) {
if ($.idealPostcodes.validatePostcodeFormat(postcode)) {
var success = function (data) {
Idpc.handle_api_success(data);
$.event.trigger("completedJsonp"); // added for API testing, better solution needed
// To introduce callback
if (Idpc.onLookupSuccess) {
Idpc.onLookupSuccess(data);
}
};
var error = function () {
Idpc.show_error("Unable to connect to server");
$.event.trigger("completedJsonp");
// To introduce callback
if (Idpc.onLookupError) {
Idpc.onLookupError();
}
};

@@ -200,8 +217,2 @@ $.idealPostcodes.lookupPostcode(postcode, Idpc.api_key, success, error);

// Test for valid postcode format
valid_postcode: function (postcode) {
var regex = /^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/;
return !!postcode.match(regex);
},
// Callback if JSONP request returns with code 2000

@@ -286,3 +297,3 @@ handle_api_success: function (data) {

var data = Idpc.result;
return $address_dropdown.change(function () {
$address_dropdown.change(function () {
var index = $(this).val();

@@ -292,3 +303,7 @@ if (index >= 0) {

}
if (Idpc.onAddressSelected) {
Idpc.onAddressSelected.call(this, data[index]);
}
});
return $address_dropdown;
},

@@ -315,2 +330,3 @@

$.idealPostcodes = {
// Expost defaults for testing

@@ -326,2 +342,6 @@ defaults: function () {

validatePostcodeFormat: function (postcode) {
return !!postcode.match(/^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/);
},
// Lookup postcode on API

@@ -348,2 +368,19 @@ lookupPostcode: function (postcode, api_key, success, error) {

},
clearAll: function () {
Idpc.$context = null;
if (Idpc.$input) {
Idpc.$input.remove();
}
if (Idpc.$button) {
Idpc.$button.remove();
}
if (Idpc.$dropdown) {
Idpc.$dropdown.remove();
}
if (Idpc.$error_message) {
Idpc.$error_message.remove();
}
}
};

@@ -353,3 +390,3 @@

$.fn.setupPostcodeLookup = function (options) {
Idpc.setup_dropdown($(this), options);
Idpc.setupPostcodeInput($(this), options);
return this;

@@ -356,0 +393,0 @@ };

@@ -25,16 +25,29 @@ (function($) {

module('jQuery#idealPostcodes');
var $input_field, $lookup_button, $dropdown,
defaults = $.idealPostcodes.defaults;
var defaults, $input_field, $lookup_button;
module('idealPostcodes.validatePostcodeFormat');
test('correctly ascertains validity of submitted postcode format', 5, function () {
equal($.idealPostcodes.validatePostcodeFormat('ID11QD'), true);
equal($.idealPostcodes.validatePostcodeFormat('id11qd'), true);
equal($.idealPostcodes.validatePostcodeFormat('id1 1qd'), true);
equal($.idealPostcodes.validatePostcodeFormat('ID1 1QD'), true);
equal($.idealPostcodes.validatePostcodeFormat('IDDQD'), false);
});
module('jQuery#setupPostcodeLookup', {
setup: function () {
$.idealPostcodes.setup({
api_key: 'api_key',
disable_interval: 0
});
$('#postcode_lookup_field').setupPostcodeLookup();
defaults = $.idealPostcodes.defaults();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
}
});
test('has postcode input box', 6, function () {
$.idealPostcodes.setup({
api_key: 'api_key',
disable_interval: 0
});
$('#postcode_lookup_field').setupPostcodeLookup();
defaults = $.idealPostcodes.defaults();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
ok($input_field.length, "there appears to be an input");

@@ -51,11 +64,2 @@ ok($lookup_button.length, "there appears to be button");

test('postcode validation', 2, function () {
$.idealPostcodes.setup({
api_key: 'api_key',
disable_interval: 0
});
$('#postcode_lookup_field').setupPostcodeLookup();
defaults = $.idealPostcodes.defaults();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
$input_field.val("BOGUSPOSTCODE");

@@ -65,22 +69,32 @@ $lookup_button.trigger("click");

strictEqual($("#" + defaults.error_message_id).html(), defaults.error_message_invalid_postcode,"it has the correct error message");
});
});
// Testing below this point requires an API key to work
/*
*
* Everything below this point requires an API key to work as it connects
* with the Ideal Postcodes API
*
* Requires a functioning api key. This will not consume any lookups
*
*/
/* Requires a functioning api key. This will not consume any lookups
var api_key = "";
module("Postcode lookups", {
setup: function () {
$.idealPostcodes.setup({
api_key: "iddqd",
disable_interval: 0,
onLookupSuccess: function () {
$.event.trigger("completedJsonp");
},
onLookupError: function () {
$.event.trigger("completedJsonp");
}
});
$('#postcode_lookup_field').setupPostcodeLookup();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
}
});
var $dropdown;
asyncTest('successful postcode lookup', 7, function () {
$.idealPostcodes.setup({
api_key: api_key,
disable_interval: 0
});
$('#postcode_lookup_field').setupPostcodeLookup();
defaults = $.idealPostcodes.defaults();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
asyncTest('successful postcode lookup', 5, function () {
$input_field.val("ID11QD");

@@ -94,3 +108,3 @@ $lookup_button.trigger("click");

$dropdown.val("5").trigger("change"); // Select 3 lined output
[defaults.output_fields.line_1, defaults.output_fields.line_2, defaults.output_fields.line_3, defaults.output_fields.post_town, defaults.output_fields.postcode].forEach(function (elem) {
[defaults.output_fields.line_1, defaults.output_fields.post_town, defaults.output_fields.postcode].forEach(function (elem) {
ok($(elem).val(), elem + " has content");

@@ -100,14 +114,19 @@ });

});
asyncTest('no postcode result', 2, function () {
$.idealPostcodes.setup({
api_key: api_key,
disable_interval: 0
asyncTest("Postcode lookup cleanup", 6, function () {
$input_field.val("ID11QD");
$lookup_button.trigger("click");
$(document).off("completedJsonp").on("completedJsonp", function () {
start();
notEqual($("#" + defaults.input_id).length, 0);
notEqual($("#" + defaults.dropdown_id).length, 0);
notEqual($("#" + defaults.button_id).length, 0);
$.idealPostcodes.clearAll();
equal($("#" + defaults.input_id).length, 0);
equal($("#" + defaults.dropdown_id).length, 0);
equal($("#" + defaults.button_id).length, 0);
});
$('#postcode_lookup_field').setupPostcodeLookup();
defaults = $.idealPostcodes.defaults();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
});
asyncTest('Postcode not found result', 2, function () {
$input_field.val("ID11QE");

@@ -122,4 +141,79 @@ $lookup_button.trigger("click");

*/
asyncTest("Postcode lookup should be triggered by enter key in input box", 6, function () {
$input_field.val("ID11QD");
var e = $.Event('keypress');
e.which = 13;
$input_field.trigger(e);
$(document).off("completedJsonp").on("completedJsonp", function () {
start();
notEqual($("#" + defaults.input_id).length, 0);
notEqual($("#" + defaults.dropdown_id).length, 0);
notEqual($("#" + defaults.button_id).length, 0);
$.idealPostcodes.clearAll();
equal($("#" + defaults.input_id).length, 0);
equal($("#" + defaults.dropdown_id).length, 0);
equal($("#" + defaults.button_id).length, 0);
});
});
asyncTest("Postcode lookup cleanup with error message", 6, function () {
$input_field.val("ID11QE");
$lookup_button.trigger("click");
$(document).off("completedJsonp").on("completedJsonp", function () {
start();
notEqual($("#" + defaults.input_id).length, 0);
notEqual($("#" + defaults.error_message_id).length, 0);
notEqual($("#" + defaults.button_id).length, 0);
$.idealPostcodes.clearAll();
equal($("#" + defaults.input_id).length, 0);
equal($("#" + defaults.error_message_id).length, 0);
equal($("#" + defaults.button_id).length, 0);
});
});
module("Callbacks to postcode lookup", {
setup: function () {
$.idealPostcodes.setup({
api_key: "iddqd",
disable_interval: 0,
onLookupSuccess: function (data) {
$.event.trigger("completedJsonp", [data]);
},
onAddressSelected: function (selectedData) {
$.event.trigger("addressSelected", [selectedData]);
}
});
$('#postcode_lookup_field').setupPostcodeLookup();
$input_field = $("#"+defaults.input_id);
$lookup_button = $("#"+defaults.button_id);
}
});
asyncTest("Postcode lookup triggering onLookupSuccess callback", 1, function () {
$input_field.val("ID11QD");
$lookup_button.trigger("click");
$(document).off("completedJsonp").on("completedJsonp", function (e, data) {
$(document).off("addressSelected").on("addressSelected", function (e, selectedData) {
start();
deepEqual(data.result[2], selectedData);
});
$("#idpc_dropdown").val(2).trigger("change");
});
});
module('$.lookupPostcode');
asyncTest('Successful postcode lookup', 4, function () {
var success = function (data, status, jqxhr) {
start();
equal(jqxhr.status, 200);
equal(data.code, 2000);
notEqual(data.result.length, 0);
notEqual(data.result[0].postcode, "ID11QD");
};
$.idealPostcodes.lookupPostcode("ID11QD", "iddqd", success);
});
}(jQuery));