diff --git a/compiled/occupancy/index.js b/compiled/occupancy/index.js index a21ffc5..e2e2782 100644 --- a/compiled/occupancy/index.js +++ b/compiled/occupancy/index.js @@ -1,5 +1,7 @@ "use strict"; +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } + function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } @@ -12,7 +14,7 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } -var match = function match(occupancy) { +var _match = function match(occupancy) { return !(occupancy.match(/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{1,2}?$/gi) == null) || !(occupancy.match(/^[0-9]{1,2}(-[0-9]{1,2}?(,[0-9]{1,2})*)?$/ig) == null); }; @@ -81,9 +83,50 @@ var get = function get(occupancy) { return occupancies; }; +var strummerMatcher = { + match: function match(path, value) { + var dataCheck = value; + + if (typeof value === 'string') { + if (value.split(',').every(function (occupancy) { + return !!occupancy.match(/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{1,2}?$/gi); + })) { + dataCheck = [value.split(',')].flat(); + } else { + dataCheck = [value]; + } + } else if (typeof value === 'undefined' || !value) { + dataCheck = []; + } + + var _iterator = _createForOfIteratorHelper(dataCheck), + _step; + + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + var occupancy = _step.value; + + if (!_match(occupancy)) { + return 'Invalid occupancy format'; + } + } + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } + }, + toJSONSchema: function toJSONSchema() { + return { + type: 'string', + properties: {} + }; + } +}; module.exports = { parse: parse, get: get, - match: match, - toString: toString + match: _match, + toString: toString, + strummerMatcher: strummerMatcher }; \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 6a7c030..9597204 100644 --- a/index.d.ts +++ b/index.d.ts @@ -30,6 +30,10 @@ declare module "@luxuryescapes/lib-global" { childrenAge?: Array; } + interface JSONSchema { + type: "string"; + } + export type LeHotelOfferType = | "hotel" | "last_minute_hotel" @@ -69,6 +73,7 @@ declare module "@luxuryescapes/lib-global" { parse: (occupancy: string) => Occupants; match: (occupancy: string) => boolean; toString: (occupancy: Occupants) => string; + strummerMatcher: { match: (path: string, value: any) => string | undefined, toJSONSchema?: () => JSONSchema }; }; const currency: { addDollarType: ( diff --git a/package.json b/package.json index d6062ea..c9ba1c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@luxuryescapes/lib-global", - "version": "2.4.9", + "version": "2.5.0", "description": "Lib for expanding functionality and deduplicating code between services", "main": "compiled/index.js", "types": "index.d.ts", diff --git a/src/occupancy/index.js b/src/occupancy/index.js index 83e9300..9406dc5 100644 --- a/src/occupancy/index.js +++ b/src/occupancy/index.js @@ -54,9 +54,31 @@ const get = occupancy => { return occupancies } +const strummerMatcher = { + match: (path, value) => { + let dataCheck = value + if (typeof value === 'string') { + if (value.split(',').every(occupancy => !!occupancy.match(/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{1,2}?$/gi))) { + dataCheck = [value.split(',')].flat() + } else { + dataCheck = [value] + } + } else if (typeof value === 'undefined' || !value) { + dataCheck = [] + } + for (const occupancy of dataCheck) { + if (!match(occupancy)) { + return 'Invalid occupancy format' + } + } + }, + toJSONSchema: () => ({ type: 'string', properties: {} }), +} + module.exports = { parse: parse, get: get, match: match, toString: toString, + strummerMatcher: strummerMatcher, } diff --git a/test/occupancy/index.test.js b/test/occupancy/index.test.js index 3f0a865..64a34ed 100644 --- a/test/occupancy/index.test.js +++ b/test/occupancy/index.test.js @@ -76,5 +76,23 @@ describe('Occupancy', () => { childrenAges: [], })).to.eql('4-2-1') }) + + describe('occupancy.strummerMatcher', () => { + it('allows an valid occupancy', () => { + expect(occupancy.strummerMatcher.match('', '2')).to.eql(undefined) + }) + + it('allows multiple occupancies', () => { + expect(occupancy.strummerMatcher.match('', ['2', '3-1'])).to.eql(undefined) + }) + + it('detects an invalid occupancy', () => { + expect(occupancy.strummerMatcher.match('', 'x')).to.eql('Invalid occupancy format') + }) + + it('detects an invalid occupancy in an array', () => { + expect(occupancy.strummerMatcher.match('', ['2', 'x', '3-1'])).to.eql('Invalid occupancy format') + }) + }) }) })