UNPKG

kokopu

Version:

A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.

162 lines 6.96 kB
"use strict"; /*! * -------------------------------------------------------------------------- * * * * Kokopu - A JavaScript/TypeScript chess library. * * <https://www.npmjs.com/package/kokopu> * * Copyright (C) 2018-2025 Yoann Le Montagner <yo35 -at- melix.net> * * * * Kokopu is free software: you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * * as published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version. * * * * Kokopu is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General * * Public License along with this program. If not, see * * <http://www.gnu.org/licenses/>. * * * * -------------------------------------------------------------------------- */ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _POJOExceptionBuilder_pojo, _POJOExceptionBuilder_path; Object.defineProperty(exports, "__esModule", { value: true }); exports.POJOExceptionBuilder = void 0; exports.decodeStringField = decodeStringField; exports.decodeNumberField = decodeNumberField; exports.decodeBooleanField = decodeBooleanField; exports.decodeArrayField = decodeArrayField; exports.decodeObjectField = decodeObjectField; const exception_1 = require("../exception"); const i18n_1 = require("../i18n"); /** * Helper class to build a {@link InvalidPOJO}. */ class POJOExceptionBuilder { constructor(pojo) { _POJOExceptionBuilder_pojo.set(this, void 0); _POJOExceptionBuilder_path.set(this, []); __classPrivateFieldSet(this, _POJOExceptionBuilder_pojo, pojo, "f"); } push(fieldName) { __classPrivateFieldGet(this, _POJOExceptionBuilder_path, "f").push(fieldName); } pop() { __classPrivateFieldGet(this, _POJOExceptionBuilder_path, "f").pop(); } build(message, ...tokens) { let fieldName = ''; let isFirstPathComponent = true; for (const pathComponent of __classPrivateFieldGet(this, _POJOExceptionBuilder_path, "f")) { if (typeof pathComponent === 'number') { fieldName += `[${pathComponent}]`; } else { fieldName += isFirstPathComponent ? pathComponent : '.' + pathComponent; } isFirstPathComponent = false; } return new exception_1.InvalidPOJO(__classPrivateFieldGet(this, _POJOExceptionBuilder_pojo, "f"), fieldName, message, ...tokens); } } exports.POJOExceptionBuilder = POJOExceptionBuilder; _POJOExceptionBuilder_pojo = new WeakMap(), _POJOExceptionBuilder_path = new WeakMap(); /** * Validate a string-valued field read from a POJO. */ function decodeStringField(pojo, fieldName, exceptionBuilder, setter) { if (!(fieldName in pojo)) { return; } exceptionBuilder.push(fieldName); const value = pojo[fieldName]; if (typeof value === 'string') { setter(value); } else if (value !== undefined) { throw exceptionBuilder.build(i18n_1.i18n.INVALID_POJO_STRING_FIELD); } exceptionBuilder.pop(); } /** * Validate a number-valued field read from a POJO. */ function decodeNumberField(pojo, fieldName, exceptionBuilder, setter) { if (!(fieldName in pojo)) { return; } exceptionBuilder.push(fieldName); const value = pojo[fieldName]; if (typeof value === 'number') { setter(value); } else if (value !== undefined) { throw exceptionBuilder.build(i18n_1.i18n.INVALID_POJO_NUMBER_FIELD); } exceptionBuilder.pop(); } /** * Validate a boolean-valued field read from a POJO. */ function decodeBooleanField(pojo, fieldName, exceptionBuilder, setter) { if (!(fieldName in pojo)) { return; } exceptionBuilder.push(fieldName); const value = pojo[fieldName]; if (typeof value === 'boolean') { setter(value); } else if (value !== undefined) { throw exceptionBuilder.build(i18n_1.i18n.INVALID_POJO_BOOLEAN_FIELD); } exceptionBuilder.pop(); } /** * Validate an array-valued field read from a POJO. */ function decodeArrayField(pojo, fieldName, exceptionBuilder, setter) { if (!(fieldName in pojo)) { return; } exceptionBuilder.push(fieldName); const value = pojo[fieldName]; if (Array.isArray(value)) { setter(value); } else if (value !== undefined) { throw exceptionBuilder.build(i18n_1.i18n.INVALID_POJO_ARRAY_FIELD); } exceptionBuilder.pop(); } /** * Validate an object-valued field read from a POJO. */ function decodeObjectField(pojo, fieldName, exceptionBuilder, setter) { if (!(fieldName in pojo)) { return; } exceptionBuilder.push(fieldName); const value = pojo[fieldName]; if (typeof value === 'object' && value !== null) { setter(value); } else if (value !== undefined) { throw exceptionBuilder.build(i18n_1.i18n.INVALID_POJO_OBJECT_FIELD); } exceptionBuilder.pop(); } //# sourceMappingURL=pojo_util.js.map