From 82329159517fb4117ae39b127e6a8469b580a5f2 Mon Sep 17 00:00:00 2001 From: Sean Morris <640101+seanmorris@users.noreply.github.com> Date: Thu, 5 Sep 2024 14:02:37 -0400 Subject: [PATCH] README. --- README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f7af1e7..eb998c8 100644 --- a/README.md +++ b/README.md @@ -389,7 +389,7 @@ Map one or more properties to a Record. ```javascript import { Schema as s } from 'libtuple'; -const companySchema = s.sDict({ +const companySchema = s.sRecord({ name: s.string(), phone: s.string(), address: s.string(), @@ -400,16 +400,48 @@ const company = companySchema({ phone: '+1-000-555-1234', address: '123 Fake St, Anytown, USA', }); + +console.log({company}); ``` #### Schema.dict(properties) Map one or more values to a Dict. +```javascript +import { Schema as s } from 'libtuple'; + +const companySchema = s.sDict({ + name: s.string(), + phone: s.string(), + address: s.string(), +}); + +const company = companySchema({ + name: 'Acme Corporation', + phone: '+1-000-555-1234', + address: '123 Fake St, Anytown, USA', +}); + +console.log({company}); +``` + #### Schema.nTuple(...values) Map n values to a Tuple. Will append each value in the input to the Tuple using the same mapper. +```javascript +import { Schema as s } from 'libtuple'; + +const vectorSchema = s.nTuple(s.number()); + +const vec2 = vectorSchema([5, 10]); +const vec3 = vectorSchema([5, 10, 11]); +const vec4 = vectorSchema([5, 10, 11, 17]); + +console.log({vec2, vec3, vec4}); +``` + #### Schema.nGroup(...values) Map n values to a Group. Will append each value in the input to the Group using the same mapper. @@ -421,7 +453,7 @@ Map n properties to a Record. Will append additional properties without mapping ```javascript import { Schema as s } from 'libtuple'; -const companySchema = s.sDict({ +const companySchema = s.nRecord({ name: s.string(), phone: s.string(), address: s.string(), @@ -432,6 +464,7 @@ const company = companySchema({ phone: '+1-000-555-1234', address: '123 Fake St, Anytown, USA', openHours: "9AM-7PM", + slogan: "We do business.", }); ``` @@ -460,6 +493,31 @@ Strictly map values to a Group. Will throw an error if the number of values does Strictly map values to a Record. Will throw an error if the number of values does not match. +```javascript +import { Schema as s } from 'libtuple'; + +const companySchema = s.nRecord({ + name: s.string(), + phone: s.string(), + address: s.string(), +}); + +const company = companySchema({ + name: 'Acme Corporation', + phone: '+1-000-555-1234', + address: '123 Fake St, Anytown, USA', +}); + +// ERROR! +companySchema({ + name: 'Acme Corporation', + phone: '+1-000-555-1234', + address: '123 Fake St, Anytown, USA', + openHours: "9AM-7PM", + slogan: "We do business.", +}); +``` + #### Schema.sDict(properties) Strictly map values to a Dict. Will throw an error if the number of values does not match. @@ -488,6 +546,24 @@ Exclusively map values to a Group. Will drop any keys not present in the schema. #### Schema.xRecord(properties) +```javascript +const companySchema = s.xRecord({ + name: s.string(), + phone: s.string(), + address: s.string(), +}); + +const company = companySchema({ + name: 'Acme Corporation', + phone: '+1-000-555-1234', + address: '123 Fake St, Anytown, USA', + openHours: "9AM-7PM", + slogan: "We do business.", +}); + +console.log({company}); +``` + Exclusively map values to a Record. Will drop any keys not present in the schema. #### Schema.xDict(properties)