note
Version:
Create timestamped markdown notes from the command line
122 lines (120 loc) • 4.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.looksLikeFlag = exports.looksLikeFilename = exports.TitleWord = exports.TitleInput = void 0;
var _Slug = require("@effect-native/schemas/Slug");
var Array = _interopRequireWildcard(require("effect/Array"));
var Schema = _interopRequireWildcard(require("effect/Schema"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
* Argument validation utilities for the note CLI.
*
* @since 0.1.0
*/
/**
* Checks if an argument looks like a filename (has dot, no spaces).
*
* Returns true if the argument contains a dot AND has no spaces.
* This catches cases like "file.md", "test.txt", "config.json".
*
* @example
* import { looksLikeFilename } from "note/Validate"
*
* looksLikeFilename("file.md") // true
* looksLikeFilename("test.txt") // true
* looksLikeFilename("hello world") // false
*
* @since 0.1.0
* @category Validation
*/
const looksLikeFilename = arg => arg.includes(".") && !arg.includes(" ");
/**
* Checks if an argument looks like a flag (starts with - or --, or contains =).
*
* Returns true if the argument:
* - Starts with "-" (short flag or long flag)
* - Contains "=" (key=value syntax)
*
* @example
* import { looksLikeFlag } from "note/Validate"
*
* looksLikeFlag("--help") // true
* looksLikeFlag("-v") // true
* looksLikeFlag("key=value") // true
* looksLikeFlag("hello") // false
*
* @since 0.1.0
* @category Validation
*/
exports.looksLikeFilename = looksLikeFilename;
const looksLikeFlag = arg => arg.startsWith("-") || arg.includes("=");
/**
* Schema that validates a string is a plain title word.
*
* Rejects strings that look like:
* - Flags (starting with `-` or `--`, or containing `=`)
* - Filenames (containing `.` with no spaces)
*
* @example
* import * as Schema from "effect/Schema"
* import { TitleWord } from "note/Validate"
*
* // Valid title words pass through
* Schema.decodeUnknownSync(TitleWord)("hello") // "hello"
* Schema.decodeUnknownSync(TitleWord)("my-title") // "my-title"
*
* @since 0.1.0
* @category Schema
*/
exports.looksLikeFlag = looksLikeFlag;
const TitleWord = exports.TitleWord = /*#__PURE__*/Schema.String.pipe(/*#__PURE__*/Schema.filter(s => !looksLikeFlag(s), {
message: s => ({
message: `"${s.actual}" looks like a flag. This tool doesn't accept flags yet.\nUsage: note <title words...>`,
override: true
})
}), /*#__PURE__*/Schema.filter(s => !looksLikeFilename(s), {
message: s => ({
message: `"${s.actual}" looks like a filename. This tool creates filenames automatically.\nUsage: note <title words...>`,
override: true
})
}));
/**
* Schema for mutable non-empty tuple [string, ...string[]] for @effect/cli compatibility.
*
* @since 0.1.0
* @category Schema
*/
const MutableNonEmptyTitleWords = /*#__PURE__*/Schema.mutable(/*#__PURE__*/Schema.Tuple([TitleWord], TitleWord));
/**
* Schema that transforms a non-empty array of validated title words
* into a TitleInput containing the derived title and slug.
*
* Uses mutable tuple type for compatibility with @effect/cli Args.atLeast(1).
*
* @example
* import * as Schema from "effect/Schema"
* import { TitleInput } from "note/Validate"
*
* const result = Schema.decodeUnknownSync(TitleInput)(["hello", "world"])
* // result: { words: ["hello", "world"], title: "hello world", slug: "hello-world" }
*
* @since 0.1.0
* @category Schema
*/
const TitleInput = exports.TitleInput = /*#__PURE__*/Schema.transform(MutableNonEmptyTitleWords, /*#__PURE__*/Schema.Struct({
words: /*#__PURE__*/Schema.NonEmptyArray(Schema.String),
title: Schema.String,
slug: Schema.String
}), {
strict: true,
decode: words => ({
words,
title: Array.join(words, " "),
slug: (0, _Slug.slugify)(Array.join(words, " "))
}),
encode: ({
words
}) => words
});
//# sourceMappingURL=Validate.js.map