Skip to content

Commit

Permalink
improve Cow type definition for moo, cowthink and cowsay functions
Browse files Browse the repository at this point in the history
version are taken from package.json file and year is dynamic for the cli help messages
  • Loading branch information
erincones committed Oct 13, 2022
1 parent 1b6da26 commit 45adba8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 43 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Erick Rincones
Copyright (c) 2022 Erick Rincones

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
46 changes: 25 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ commands, once installed, run `cowsayjs -h`, `cowthinkjs -h` or `moojs -h` to
print the help.

```Text
moojs, cowsayjs, cowthinkjs v1.1.3
Copyright (c) 2021 Erick Rincones
moojs, cowsayjs, cowthinkjs v1.1.4
Copyright (c) 2022 Erick Rincones
Licensed under the MIT License
A nodejs clone of the classic cowsay and cowthink cli commands.
Expand Down Expand Up @@ -252,7 +252,7 @@ moo({
```Text
__________________________
( I need to sleep )
( 2022-10-07T22:57:16.496Z )
( 2022-10-13T19:18:51.885Z )
--------------------------
o ^__^
o (@@)\_______
Expand All @@ -263,37 +263,41 @@ moo({

```Typescript
// Custom cow
cowthink({
cowsay({
message: `No way, I don't want the leftovers of your love.`,
eyes: `••`,
cow: {
name: `custom`,
template: [
` \\`,
` \\`,
` //`,
` ('>`,
` /rr`,
` *\))_`
` \\\\`,
` \\\\`,
`(\\__/) ||`,
`(oㅅo) ||`,
`/ づ`
],
actionPos: [
[ 0, 2 ],
[ 1, 3 ]
[ 0, 5 ],
[ 0, 6 ],
[ 1, 6 ],
[ 1, 7 ]
],
eyesPos: [
[ 3, 1 ],
[ 3, 3 ]
]
}
});
```

```Text
_______________________________________
( No way, I don't want the leftovers of )
( your love. )
/ No way, I don't want the leftovers of \
\ your love. /
---------------------------------------
o
o
//
('>
/rr
*))_
\\
\\
(\__/) ||
(•ㅅ•) ||
/ づ
```


Expand Down
24 changes: 19 additions & 5 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

var fs = require("fs");
var path = require("path");

var lib = require("../lib");
var mode = require("../lib/mode");
var cows = require("../cows");
Expand Down Expand Up @@ -31,6 +34,20 @@ var cows = require("../cows");
*/


/**
* Parsed `package.json` file
*
* @type {Record<string, string | string[] | Record<string, string>>}
* @package
*/
var package_json = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));

/**
* Current year
*/
var year = new Date().getFullYear();


/**
* Get all available cow mode data IDs
*
Expand Down Expand Up @@ -81,18 +98,15 @@ function getArg(token, j, argv, i) {
* @package
*/
function printHelp() {
// Version
var version = "1.1.3";

// Get current script
var script = process.argv[1].replace(/\\/g, "/");
script = script.slice(script.lastIndexOf("/") + 1).replace(/\./, "");


// Script info
var info =
"moojs, cowsayjs, cowthinkjs v" + version + "\n" +
"Copyright (c) 2021 Erick Rincones\n" +
"moojs, cowsayjs, cowthinkjs v" + package_json.version + "\n" +
"Copyright (c) " + year + " Erick Rincones\n" +
"Licensed under the MIT License\n\n";

// Brief
Expand Down
16 changes: 11 additions & 5 deletions cows/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ export declare type CowAction = `o` | `\\`;


/**
* Cow
* Cow without name
*/
export declare interface Cow {
/** Cow name */
name: string;
export declare interface CowBase {
/** Default eyes for empty string */
defEyes?: string;
/** Default tongue for empty string */
Expand All @@ -35,6 +33,14 @@ export declare interface Cow {
tonguePos?: ReadonlyArray<Position>;
}

/**
* Cow
*/
export declare interface Cow extends CowBase {
/** Cow name */
name: string;
}


/**
* Cows list
Expand Down Expand Up @@ -81,4 +87,4 @@ export declare function removeCow(name: string): Cow | undefined;
* @param tongue Tongue
* @returns Rendered cow
*/
export declare function renderCow(cow: Cow, action: CowAction, eyes?: string, tongue?: string): string;
export declare function renderCow(cow: CowBase, action: CowAction, eyes?: string, tongue?: string): string;
21 changes: 17 additions & 4 deletions cows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ var utils = require("../lib/utils");
* @typedef {"o" | "\\"} CowAction
*/


/**
* Cow
* Cow name
*
* @typedef {Object} Cow
* @typedef {Object} CowName
* @property {string} name Cow name
* @package
*/

/**
* Cow without name
*
* @typedef {Object} CowBase
* @property {string} [defEyes] Default eyes
* @property {string} [defTongue] Default tongue
* @property {ReadonlyArray<string>} template Cow template
* @property {ReadonlyArray<Position>} [actionPos] Action position indexes
* @property {ReadonlyArray<Position>} [eyesPos] Eyes position indexes
* @property {ReadonlyArray<Position>} [tonguePos] Tongue position indexes
* @package
*/

/**
* Cow
*
* @typedef {CowBase & CowName} Cow
*/


Expand Down Expand Up @@ -266,7 +279,7 @@ function removeCow(name) {
/**
* Cow renderer function
*
* @param {Cow} cow Cow to render
* @param {CowBase} cow Cow to render
* @param {CowAction} [action] Action
* @param {string} [eyes] Eyes
* @param {string} [tongue] Tongue
Expand Down
6 changes: 2 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";


var box = require("./box");
var mode = require("./mode");
var cows = require("../cows");
Expand All @@ -10,7 +9,7 @@ var cows = require("../cows");
* Common options for cowsay and cowthink functions
*
* @typedef {Object} CowOptions
* @property {string | import("../cows").Cow} [cow] Cow name
* @property {string | import("../cows").CowBase} [cow] Cow name
* @property {string} [mode] Cow face mode
* @property {string} [eyes] Custom cow eyes
* @property {string} [tongue] Custom cow tongue
Expand Down Expand Up @@ -142,15 +141,14 @@ function moo(message, options) {
}

// Get cow and returns
/** @type {import("../cows").Cow} */
/** @type {import("../cows").CowBase} */
var cow;
var valid = true;

switch (typeof opts.cow) {
case "string": cow = cows.getCow(opts.cow); break;
case "object":
valid = valid && opts.cow !== null;
valid = valid && typeof opts.cow.name === "string";
valid = valid && Array.isArray(opts.cow.template);
valid = valid && opts.cow.template.every(function(line) { return typeof line === "string"; });
valid = valid && (opts.cow.defEyes === undefined || typeof opts.cow.defEyes === "string");
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cowsayjs",
"version": "1.1.3",
"version": "1.1.4",
"description": "A nodejs clone of the classic cowsay and cowthink cli commands",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 45adba8

Please sign in to comment.